EnableViewState="False" Does not take effect?

K

kai

Hi, All

I create an ASP.NET page, it contains FirstName textbox and LastName
textbox. I setup "enableViewState=false" in page directive. When I enter
data in FirstName and LastName textbox, after I refresh the page or go to
different page and come back to the same page, I find data entered in
FirstName and LastName textbox are still there.

I think after setup "enableViewState=false" , we are not suppose to see data
in the textbox after we move to another page and come back?

Please help.

Thanks

Kai
 
T

Teemu Keiski

Hi,

disabling ViewState has no effect on TextBox..

Take a look at my blog post for an explanation
http://blogs.aspadvice.com/joteke/archive/2004/03/15/767.aspx

This relates to the posting behaviour (when page posts back to itself), but
you said "come back to the same page". Are you visiting another completely
separate ASP.NET page and then return to the original Page containing
TextBoxes and they still keep the state!?
 
S

Steven Cheng[MSFT]

Hi Kai,

From your description, you found that the ASP.NET textbox control's entered
value will remain after posted back no matter the page's viewstate is
enabled or not ,yes?

As for this problem, here are my understanding:
In fact ,the page's viewstate is really disabled. And any setting on the
serverside won't be able to remain between post backs. For example, you try
the following code(when page's viewstate is disabled):
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
TextBox1.TextMode = TextBoxMode.Password;
}
}

Then, when the page is first time loaded, the TextBox1 is a password mode.
If you post back the page(add a submit button), you'll find the TextBox1's
mode turn back to normal(singleline).

Then, you must be confused why the value in the textbox remains after post
back, yes? This is because the entered value is rendered out with the
TextBox after post back. For example, when the page is posted back, at the
serverside, the textbox will pass through the following steps:
1. its control instance be contructed

2. Restore its states from the viewstate

3. Compare the states with its new inputed value( this is the new value we
get which is actually in the Request.Form collection)

4. Change the textbox's state(value) to the new value

5.... process the textbox's postback event( if exist)

6. The textbox is rendered with the new value (when the page is
renderedout).

The #6 is the key point why the new value will remain , because the textbox
is rendered out as html element which has contain the new value such as
<input type="text" value="new value" />, no viewstate needed. Do you think
so?

If you have anything 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.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
K

kai

Hi, Teemu
Thanks for helping me. I read your page explanation.
Yes, I visit completely another ASP.NET page, when I navigate back, data
still ramain in the textboxs and radio button.
Only password field data cleared.

Thanks
Kai
 
K

kai

Hi, Steven
Thanks for helping me. I am learning and try to understand your
explanation. If I have questions, I will postback to you for help.

Thanks again


Kai
 
T

Teemu Keiski

OK,

by navigating back do you use Back button of browser or do you use explicit
link (at the another page) to get back to the page with TextBoxes?

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke

Hi, Teemu
Thanks for helping me. I read your page explanation.
Yes, I visit completely another ASP.NET page, when I navigate back, data
still ramain in the textboxs and radio button.
Only password field data cleared.

Thanks
Kai
 
K

kai

I use browser link to go back to the page which has the textbox.
After I read your help, I change to explicit link, then the textbox and
radio button lose all the data. It seems to me ViewState = False take effect
when use explicit link to navigate. Why?

Thanks

Kai
 
K

kai

When I use explicit link, I lose all the data it does not matter ViewState =
True or False. So is it true ViewState=true cannot maintain data if we use
explicit link?


Thanks

Kai
 
S

Steven Cheng[MSFT]

Hi Kai,

Does the "Explicit Link" means a hyperlink? If so I think this behavior is
completely nothing related to the Page's viewstate. Because when we click a
link and navigator to a page, this page will be requested again from the
server(just like we use response.redirect(page url)), that means it is a
new request no relation with the former postback, so we can see all the
data in it lost.

In addition, here are some refernce on ASP.NET's viewstate in MSDN:
Taking a Bite Out of ASP.NET ViewState
http://msdn.microsoft.com/library/en-us/dnaspnet/html/asp11222001.asp?frame=
true

#Maintaining State in a Control
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconmantainingstateinc
ontrol.asp?frame=true

ASP.NET ServerControl's life cycle on serverside:
#Control Execution Lifecycle
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcontrolexecutionli
fecycle.asp?frame=true

Hope helps.

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.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top