当应的EnableViewState可以在服务器控件启用? [英] When should EnableViewState be enabled on a server control?

查看:182
本文介绍了当应的EnableViewState可以在服务器控件启用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有当视图状态应在服务器控件启用任何指引或规则?以及何时应

Is there any guideline or rule for when the view state should be enabled on a server control? And when it should not?

我一直在寻找这个<一个href=\"http://quickstarts.asp.net/QuickStartv20/util/srcview.aspx?path=~/aspnet/samples/data/GridViewMasterDetailsInsert.src\"><$c$c>SqlDatasource例如并注意到,该标签控件的视图状态未启用:

I was looking at this SqlDatasource example and noticed that the view state for the label control is not enabled:

<asp:Label ID="ErrorMessageLabel" EnableViewState="false" runat="server" />

为什么没有在标签控件启用的EnableViewState ?我知道,使视图状态进行一定的开销,所以我想用它仅在需要时它。

Why isn't EnableViewState enabled on the label control? I know that enabling the view state carries some overhead so I would like to use it only when it is needed.

推荐答案

下面是一个很好的经验法则:如果你(1)在$ C $变动c隐藏属性值,和(2)需要知道什么你在后面的回发不重新计算值设置值,那么你需要使用的ViewState。

Here's a good rule of thumb: If you (1) change a property's value in the code-behind, and (2) need to know what value you set in a later postback without recalculating the value, then you need to use ViewState.

例如。在我的网页的标记我可能有这样的指定Label控件:

For example. In my page's markup I might have a Label control specified like this:

<asp:Label ID="TitleLabel" runat="server" Text="Update this Employee" />

然后在Page_Load事件我有这个code:

Then in the Page_Load event I have this code:

If Not IsPostBack AndAlso myEmployeeObject.IsNew Then TitleLabel.Text = "Create a new Employee"

通过更改文本属性的值,我引入了一个新元素放入ViewState中。如果我任何后续的回传中获取标签的Text属性的值,该值将是创建一个新的员工。

By changing the value of the Text property, I've introduced a new element into ViewState. If I get the value of the Label's Text property during any subsequent PostBack, the value will be "Create a new Employee".

下面是我做的,当我开始尽量减少ViewState中的我的网页使用量。我使在页面上跟踪。跟踪输出被添加到您的页面的底部时,其浏览器中呈现。跟踪输出识别页面上的每一个服务器控件,包括有多少的ViewState(以字节为单位,我相信)每个控制门店。我用这个信息来计算时,我想交易的ViewState的开销重新计算值的开销。

Here's what I do when I set out to minimize the amount of ViewState used by my page. I enable tracing on the page. The trace output is added to the bottom of your page when its rendered in the browser. The trace output identifies every single server control on your page and includes how much ViewState (measured in bytes, I believe) each control stores. I use this information to calculate when I want to trade the overhead of ViewState for the overhead of recalculating values.

在我的previous例子中,我将选出重新计算每一个回发标签的Text属性和停止存储在ViewState中的Text属性。这是我的更新标记的样子:

In my previous example, I would elect to recalculate the Label's Text property on every PostBack and stop storing the Text property in ViewState. This is how my updated markup would look:

<asp:Label ID="TitleLabel" runat="server" Text="Update this Employee" EnableViewState="false" />

和我的更新Page_Load事件:

And my updated Page_Load event:

If myEmployeeObject.IsNew Then TitleLabel.Text = "Create a new Employee"

这篇关于当应的EnableViewState可以在服务器控件启用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆