Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

c# - How I can deactivate ViewState without Control problems

I wrote a ASP.NET Application and it run in IIS7 of a Server. If I open this webform in my Browser and show me the Sitecode I see this...

enter image description here

I have many Controls how Buttons,Labels,TextBoxes and a ListView. I try to deactivate ViewState in the web.config but if I deactivate this my Application don't run correctly. What can I do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...