How I can deactivate ViewState without Control problems

二次信任 提交于 2019-11-28 14:00:08
Aristos

Deactivate only the controls that not need the viewstate.

To do that you need to understand what the viewstate is.

Viewstate is where the page save and remember the values of the controls to have them after a post back. Remember that, the viewstate is used after a post back.

So actually you have two times the same data, but only the viewstate is post back the previous data and code behind can be use that data.

So the main question is, what controls do you need to be remember what you have fill them in, or what controls need to remeber the previous state of them.

Lets see a simple Literal with EnableViewState on and off.

ViewState ON

<asp:Literal runat="server" EnableViewState="true" ID="txtLiterar">

Now if you place a text on this literal the text is also saved on viewstate and on code behind you can do that.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        txtLiterar.Text = "Hello There";
    }
}

So after the post back the Literal still have its content, and you can avoid to fill it again, because the viewstate have it and automatically fills it again.

ViewState OFF

<asp:Literal runat="server" EnableViewState="false" ID="txtLiterar">

Now if you place a text on this literal the text is not saved on view state and on code behind you add it as.

protected void Page_Load(object sender, EventArgs e)
{
    txtLiterar.Text = "Hello There";
}

So the different is that you need to always fill that control with data on every post.

Where the viewstate is needed most.

The most needed part of the viewstate is when you fill a dropdown list. There you have a databind and code behind need to remember the values to place on the SelectValue the correct one.

Its also needed on GridView and other controls like that because is keep the previous page and other information's when you paging your data.

So you can close on most of your controls the viewstate - on that controls that you can fill them again on every post back, and on that controls that not need to remeber the previous state.

More to read:
How to optimize class for viewstate
Determine size of ASP.NET page's viewstate before serving page
Limiting view state information on AJAX calls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!