Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 what does enableviewstate do exactly?

Author  Topic 

rtutus
Aged Yak Warrior

522 Posts

Posted - 2006-05-21 : 17:48:26
I tried enableviewstate=true and =false for both my age and my textBox control, but i didn t notice any difference in the value of my textBox controls. They always come back with the initial "text" property that I set for the textbox control when i design the page.

My question: When does the enableviewstate property make a difference? In what scenario does it matter whether we set it on true or False

Thanks.

dfiala
Posting Yak Master

116 Posts

Posted - 2006-05-21 : 19:51:45
You are probably reloading the page on postback.

Make sure you only fill your controls when

Not Page.IsPostBack

Viewstate keeps the control values through the postback, so there is no need (in this case) to refill the controls.

Here's some more info on ViewState
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp



Dean Fiala
Very Practical Software, Inc
Now with Blogging...
http://www.vpsw.com/blogbaby
Microsoft MVP
Go to Top of Page

rtutus
Aged Yak Warrior

522 Posts

Posted - 2006-05-21 : 22:28:35
That s really good article, check this paragraph:
Notice that in our discussion on the load postback data stage, there was no mention of view state. You might naturally be wondering, therefore, why I bothered to mention the load postback data stage in an article about view state. The reason is to note the absence of view state in this stage. It is a common misconception among developers that view state is somehow responsible for having TextBoxes, CheckBoxes, DropDownLists, and other Web controls remember their values across postback. This is not the case, as the values are identified via posted back form field values, and assigned in the LoadPostData() method for those controls that implement IPostBackDataHandler.

Go to Top of Page

JBelthoff
Posting Yak Master

173 Posts

Posted - 2006-05-23 : 08:13:04
enableviewstate=true allows you to add things to the viewstate.

Make a form with 2 buttons and a text box.

Take a look at the following code. Run it with enableviewstate=true and then enableviewstate=false. You will see the difference.

The first button adds the value of the Textbox to the ViewState. The second reads and wrties it from the ViewSate. When ViewState is set to false the second button will not write the value.

ASP Page

<form id="Form1" method="post" runat="server">
<P>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></P>
<P>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P>
<P> </P>
<P>
<asp:Button id="Button2" runat="server" Text="Button"></asp:Button></P>

</form>


Code Behind

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ViewState("Text") = TextBox1.Text

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Response.Write(ViewState("Text"))
End Sub



JBelthoff
D
odge, Duck, Dip, Dive & Dodge
If a man can dodge a wrench, he can dodge a ball!
Asp Hoting Provider
Go to Top of Page
   

- Advertisement -