禁用ViewState后,WebForm还有意义吗?

禁用ViewState后,WebForm还有意义吗?

WebForm 是基于事件的开发模式,禁用ViewState 后,破坏了控件事件的完整性,那我们还会在Web.config里面禁用掉ViewState吗?这么做什么意义,会不会破坏我们WebForm的开发模式。

  

下面是禁用掉ViewState 后,控件事件对应的情况。

Repeater

操作:ItemCommand(失效)

数据:DataBinding(可用)

行为:ItemCreated(可用) , ItemDataBound(可用)

Button

操作:Click (可用), Command(待验证)

数据:DataBinding(可用)

DropDownList

操作:SelectedIndexChanged(可用),TextChanged(待验证)

数据:DataBinding(可用),DataBound(可用)

下面列举三类Asp.net 中最常用的三类应用

  

视图型

<asp:FormView ID="FormView1" runat="server">

<ItemTemplate>...</ItemTemplate>

</asp:FormView>

FormView1.DataSource = SomeDataSource;
FormView1.DataBind();

  

列表型

<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">

<ItemTemplate>

<asp:LinkButton ID="LinkButton1" runat="server" CommandName="delete" CommandArgument='<%# Eval("ID") %>'>删除</asp:LinkButton>

</ItemTemplate>

</asp:Repeater>

 
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) { ... }

表单型

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

protected void Button1_Click(object sender, EventArgs e) { ... }

这三个例子中,是否启用ViewState对于视图型、表单型应用都没有问题,列表型应用在禁用ViewState后彻底失效,三个操作带来的优势倒是挺明显就是源代码清爽了不少,页面的加载速度也快不少。

总结:Asp.net引入ViewState本来是一笔很宝贵的财富,但是如果使用不当,会照成整个页面加载速度明显下降,是否禁用ViewState还是看具体的应用场景。

posted @ 2010-12-03 13:36  安布雷拉  阅读(6199)  评论(52编辑  收藏  举报