View State Misunderstanding

P

Philip Tripp

I've read numerous sources stating that view state can be disabled per
control, and per page, but can't seem to keep web form controls from
remembering their state on a postback. I'm using VS.Net 2002, .Net
Framework 1.0 SP2.

I create a new project, and add a new web form to the project. On the
page properties, I set EnableViewState to False and verify that the @
Page directive has "enableViewState=false" in it. I then add a TextBox
control from the Web Froms, and set its EnableViewState property to
false. I add a Button control from the Web Forms, and start my project
(F5). I type in some text to the textbox, click the button so the form
submits, and expect that the text box come back empty (because view
state has been disabled), but it always comes back populated.

I am new to ASP.NET, so am I somehow misunderstanding the dozen or so
aricles I've read about view state? Is view state always remembered on
post backs? If it is always remembered, then what the heck does setting
EnableViewState to false do?
 
S

Samuel L Matzen

Philip,

What you are seeing is the expected behavior.

The ViewState will retain the state of your control if you populate it in
CodeBehind.

If you enter data into the control in the browser you sill see this entered
value in the control in the code behind.

ViewState will not change the behavior of a text box because after you enter
text into the textbox control the data is submitted to the server when you
click the button. Then the server takes the submitted value and places it
into the text property of the control. When the new page is rendered to the
broeser the text value is already set and will show up.

The place that view state will make a lot of difference is with a treeview
where the data is never submitted back to the server.

You can check to see if you have any view state by looking at the Source in
the browser. If there is view state you will see something like:

<input type="hidden" name="__VIEWSTATE"
value="dDwxNDg5OTk5MzM7Oz4itWBDehNJ4zs9hlG5LpoPAGTocw==" /

Let's do a sample with a CheckBoxList and add the items with some code.

.. Add a CheckBoxList to your page.
.. Add the following code into the Page PreRender event to add four items
the first time the page is loaded:

<code (VB.NET) >
If Me.IsPostBack = False Then

With Me.CheckBoxList1

.Items.Add("Item 1")
.Items.Add("Item 2")
.Items.Add("Item 3")
.Items.Add("Item 4")

End With

End If
</code>

.. Run the project and click on the button a number of times. Notice the
checkbox items remain on the page. Check the Source and observe the
ViewState is significantly larger:

<input type="hidden" name="__VIEWSTATE"
value="dDwtMjAwNjc4MjQzNjt0PDtsPGk8MT47PjtsPHQ8O2w8aTw1Pjs+O2w8dDx0PDtwPGw8a
TwwPjtpPDE+O2k8Mj47aTwzPjs+O2w8cDxJdGVtIDE7SXRlbSAxPjtwPEl0ZW0gMjtJdGVtIDI+O
3A8SXRlbSAzO0l0ZW0gMz47cDxJdGVtIDQ7SXRlbSA0Pjs+Pjs+Ozs+Oz4+Oz4+O2w8Q2hlY2tCb
3hMaXN0MTowO0NoZWNrQm94TGlzdDE6MTtDaGVja0JveExpc3QxOjI7Q2hlY2tCb3hMaXN0MTozO
0NoZWNrQm94TGlzdDE6Mzs+Puu8f1UYuN6JR7A8eahu7/MHkwOv" /


.. Now set the ViewState for the CheckBoxList control to False and run the
project.
.. Notice the first time the page displays the items display.
.. Click the button and observe that the checkboxlist items no longer
display. This is because the viewstate of the control was turned off and
the list items no longer exist.

-Sam Matzen




-Sam Matzen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top