enableviewstate=false question

C

Chris

I've created two textboxes and one button on my web form. I then set the
EnableViewState property of Textbox1 to False. Textbox2 is set to True.
The button has no code in it - It simply has an auto post back which reloads
the page.

I run the web application and type "test 1" into Textbox1 and "test 2" into
Textbox2. I click the button and the page reloads. Textbox1 has the value
of "test 1". Textbox2 has the value of "test 2".

Am I missing the point here - Shouldn't the value of Textbox1 now be blank
again because I set its EnableViewState to False?


Thanks in advance.
 
A

Alex Homer

In order for ASP.NET to be able to fire server-side events for "interactive"
controls (such as a TextBox) it has to be able to detect when the values
have changed. For this it requires the original value (when the page was
sent to the client) to be available on a postback, and so it has to store
this in the page (using the viewstate). Even when you turn off viewstate,
some value are still maintained for some controls. Turning off viewstate is
used to minimize the data round-tripped to the client (try it with a
DataGrid to see the effects). If you want to "blank" a Textbox you should
change the Text property during your handling of the postback.
 
S

Steven Cheng[MSFT]

Thanks a lot for Alex's informative suggestions.

Hi Chris,

As Alex has mentioned, some certain webserver controls can still post their
values when we disable its viewstate. As for the TextBox control, it is
actual rendered as an <input type=text ...> html element when the page
render back to the client browser. And the TextBox's "Text" propery is
really be stored in the ViewState. However, when the page is post back ,
the TextBox's new value will be also post back because it is represent by
the "value" attribute of the <input type=text ..> element which is
accessable from the Request.Form[] collection. For example, if we have the
following TextBox in page
<asp:TextBox id="txt" runat=server Text="Initial value"></asp:TextBox>

when render to client, it is like below:

<input type="text" id="txt" name="txt" value="intial value" />

Then ,when the client user input new value in it, the "value" attribute's
value will change to the new input and when the page is post back, the new
value will be post together and we can use
Request.Form["txt"] to get it.

That's why we can get the textbox's value though the ViewState is disabled.
Then, what does the "Text" property (in the ViewState) do? Well, that's
anothe thing, let's have a look at the TextBox's serverside lifecycle:

When a page with a TextBox control is post back, (suppose the TextBox's ID
is "txt"), then,

In the page's LoadPostData event, it will get the TextBox's new input value
from the Request.Form[] collection as I mentioned above. Also, the page
will retrieve the TextBox's old value from the ViewState and compare them,
if different , it assgin the new Value to the "Text" property of the
TextBox and later the TextBox's TExtChanged event will be fired.. So if we
disable the ViewState , everytime the page will find that the new input it
different from the old value( because the old value in viewstate doesn't
exist ) and the TextChanged event will be fired.

In addition ,here is a good tech article which demonstrate the details of
the asp.net 's VIEWSTATE :

#Understanding ASP.NET View State
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
/viewstate.asp

Also, if you have interests , you can try using the .NET Reflector tool to
have a look at the TextBox's source code , that'll explain the question
more clearly :).

Hope all these helps. If you still have anything unclear, please feel free
to post here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
C

Chris

Hi Alex,

Many thanks for the explanation. Do you know of any lists/websites that
show which values are retained in viewstate, or do you have to work it out
for yourself.

Thanks,
Chris
 
S

Steven Cheng[MSFT]

Hi Chris,

Have you also had a chance to see the suggestions in my former reply? As
for all the buildin web controls in asp.net , there hasn't such a list
which shows all the VIEWSTATE persisted properties. But generally, we could
see if I control's properties is not able to be stored in the control's
rendered html and also need to be used in the sequential post back, that
should be persistent in VIEWSTATE. Also, as I mentioned in the former
thread, using the .NET Reflector tool to have a clear look at the
webcontrol's code is sometimes much more helpful:)

If you have anything else unclear, please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
C

Chris

Hi Steven

Many thanks for your help. This explains it well - I am sorry I missed your
post yesterday - must have got distracted and thought I read it all.

I will check out the reflector tool.

Thanks,
Chris
 
S

Steven Cheng[MSFT]

Thanks for the followup Chris.

I'm glad that the suggestions are of assistance. Also, if you have any
other questions later, please feel free to post there.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top