Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > View State Misunderstanding

Reply
Thread Tools

View State Misunderstanding

 
 
Philip Tripp
Guest
Posts: n/a
 
      07-24-2004
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?

 
Reply With Quote
 
 
 
 
Saber
Guest
Posts: n/a
 
      07-24-2004
what do you mean by "it always comes back populated.
"
"Philip Tripp" <(E-Mail Removed)> wrote in message
news:eXll1%(E-Mail Removed)...
> 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?
>




 
Reply With Quote
 
 
 
 
Samuel L Matzen
Guest
Posts: n/a
 
      07-24-2004
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="dDwxNDg5OTk5MzM7Oz4itWBDehNJ4zs9hlG5LpoPAGT ocw==" /

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="dDwtMjAwNjc4MjQzNjt0PDtsPGk8MT47PjtsPHQ8O2w 8aTw1Pjs+O2w8dDx0PDtwPGw8a
TwwPjtpPDE+O2k8Mj47aTwzPjs+O2w8cDxJdGVtIDE7SXRlbSA xPjtwPEl0ZW0gMjtJdGVtIDI+O
3A8SXRlbSAzO0l0ZW0gMz47cDxJdGVtIDQ7SXRlbSA0Pjs+Pjs +Ozs+Oz4+Oz4+O2w8Q2hlY2tCb
3hMaXN0MTowO0NoZWNrQm94TGlzdDE6MTtDaGVja0JveExpc3Q xOjI7Q2hlY2tCb3hMaXN0MTozO
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


"Philip Tripp" <(E-Mail Removed)> wrote in message
news:eXll1%(E-Mail Removed)...
> 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?
>



 
Reply With Quote
 
Philip Tripp
Guest
Posts: n/a
 
      07-26-2004
Thank you for your information response, it clarifies things quite a bit.

Samuel L Matzen wrote:
> 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="dDwxNDg5OTk5MzM7Oz4itWBDehNJ4zs9hlG5LpoPAGT ocw==" /
>
> 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="dDwtMjAwNjc4MjQzNjt0PDtsPGk8MT47PjtsPHQ8O2w 8aTw1Pjs+O2w8dDx0PDtwPGw8a
> TwwPjtpPDE+O2k8Mj47aTwzPjs+O2w8cDxJdGVtIDE7SXRlbSA xPjtwPEl0ZW0gMjtJdGVtIDI+O
> 3A8SXRlbSAzO0l0ZW0gMz47cDxJdGVtIDQ7SXRlbSA0Pjs+Pjs +Ozs+Oz4+Oz4+O2w8Q2hlY2tCb
> 3hMaXN0MTowO0NoZWNrQm94TGlzdDE6MTtDaGVja0JveExpc3Q xOjI7Q2hlY2tCb3hMaXN0MTozO
> 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
>
>
> "Philip Tripp" <(E-Mail Removed)> wrote in message
> news:eXll1%(E-Mail Removed)...
>
>>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?
>>

>
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
vec - a misunderstanding Mark Hobley Perl 1 03-18-2006 07:32 PM
Misunderstanding ConnectionStrings? Shawn Wildermuth ASP .Net 6 01-11-2006 02:57 AM
How to make a week view and day view calendar just like month view calendar in .NET ? Parthiv Joshi ASP .Net Web Controls 1 07-06-2004 03:15 PM
errors in book or my misunderstanding? David K MCSE 4 12-12-2003 03:22 PM
Re: Class property misunderstanding Kevin Spencer ASP .Net 1 06-26-2003 05:28 PM



Advertisments