Textbox retains value when EnableViewState = false

J

JollyK

Hello all,
I have always been having this issue and wondering what the solution is.

When I set the enableviewstate property to false for a textbox, the textbox
always retains its value after a postback occurs. Why is this? I want the
textbox to become empty after postback. How can I do it without setting
textbox.text = "" and without doing Response.redirect to the same page.

JK
 
P

Patrice

When you submit a form to a server, the browser always send the field values
(those who are named, not disabled and inside the form) to the server. This
is how a HTML form works "by design".

The "viewstate" is just an hidden field added by ASP.NET to store extra
informations and carry them over accross multiple server side executions of
the same page. It doesn't contains field values.

Patrice
 
J

JollyK

Yes, the browser always sends the field values to the server. But after the
form is submitted, why does the asp.net textbox retain the text value. It
should be reset to the default value which is blank field. This issue only
with asp.net server controls. If i use <input type="text" name="txtname"
....... then I will not face this problem. I hope you are getting my
question.
 
J

Jim Cheshire [MSFT]

Hi JK,

That behavior is by design:

316813 PRB: Server controls persist their state when EnableViewState is set
to
http://support.microsoft.com/?id=316813

Jim Cheshire, MCSE, MCSD [MSFT]
ASP.NET
Developer Support
(e-mail address removed)

This post is provided "AS-IS" with no warranties and confers no rights.

--------------------
 
H

Hans Kesting

With asp.net you can have "server side validators" , a feature
not present in "old" asp (or jsp, or php, or ..).
When a validator blocks a page, the user gets his/her page
back, complete with all (ok, nearly all: password is an exception)
values and the now-visible validator text.
If the input gets cleared after every postback, your user would need to
retype that input every time the validator blocks submission.

You, as the developer, can decide when the data is safe in the database
and only then clear the form. You could do this by clearing every named
input, or by writing a procedure that loops through all controls on the
page, clearing them in an appropriate manner.

Hans Kesting
 
P

Peter Blum

You have mixed up two concepts: ViewState and the value of a field posted
back. The key idea here is that properties directly connected to the form
value will always get the form value assigned on post back.

The TextBox.Text property is updated from Request.Forms on post back. It
reflects the live data. ViewState preserves the last known value of several
properties.
In fact, it preserves the value of the Text property although thats so it
can determine if the OnTextChanged event needs to fire. It needs a "before"
and "after" case to do that.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top