viewstate

J

Just Me

I have a simple page and was trying to test viewstate for a textbox.

in my page_load event

If not ispostback then

textbox1.text="hello"

end if


If I turn of enableviewstate, this initially loaded test will remain after
postback. What am I doing wrong ??

Cheers
 
G

Guest

Howdy,

You can see value of the text not because of viewstate but because of
Request.Form[textboxUniqueID]. Textbox uses ViewState to serialize original
value that was set before page was rendered to the browser in order to raise
OnTextChanged event (event OnTextChanged is raised only when
Request.Form[txtID] !=
ViewState["OriginalText"] - so if you disable viewstate for the textbox you
won't be notified about any text change beetween postbacks, see it for
yourself.
But do the same exercise for a label control - you'll find Text is not
persisted during postback (this is because label control renders as <span>
tag, which is not HTML input-type control)

regards
 
G

GroupReader

I noticed the same behavior as "Just Me" above. I'm pretty sure that
what "Just Me" is trying to do would have worked in older versions of
ASP.Net (before 2.0). I thought it had to do with "Control State"
which was introduced int .Net 2.0. Control State is the same thing as
view state, but you can't choose to turn it off.
 
G

Guest

Hi there,

Once again, persitance of the TextBox.Text property has nothing to do with
ViewState (Just Me's example will work for Label or any non-input web server
control). TextBox control implements IPostBackDataHandler inetrface, that is
responsible for retreiving the data from posted Form (HTTP POST). On every
post back request (form is submitted with all input values i.e. by pressing
submit button), Text property gets its value from the submitted Form:
(this code represents exactly what happens behind the scenes)

protected virtual bool LoadPostData(string postDataKey, NameValueCollection
postCollection)
{
base.ValidateEvent(postDataKey);
string text1 = this.Text;
string text2 = postCollection[postDataKey];
if (!this.ReadOnly && !text1.Equals(text2, StringComparison.Ordinal))
{
this.Text = text2;
return true;
}
return false;
}

Everyone who has programmed in more *raw* enviroment like ASP knows what i'm
trying to explain. It's easier to understand on following example (copy and
paste it to a ASPX page):

<input type="text" id="testBox" name="testBox" value="<%=
Request.Form["testBox"] %>" />
<input type="submit" value="Do a Post Back"/>

as you can see there's no viewstate mechanizm it works exactly the same as
'Just me' presented. That's actually what happens behind the scene. But
remember it applies only to input type controls, for asp:Label Text is
actually restored from ViewState on every post back. So if you disable
ViewState this won't display "test" on postback:

<asp:Label runat=server id=label1/>

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
label1.Text = "test";
}
}

Hope it is clear now :)

Regards
 
G

GroupReader

Thanks, it acually is clear now. Now I understant *why* the textbox
behaves as it does. Thanks.

On another note: I'm *really* convinced that the behavior of the
textBox changed somwhere between ASP.Net 1.0 and ASP.Net 2.0 and that
you *used to* have to set viewstate==true to persist the value. Your
post explains why we no longer have to do this. I never knew why
before.

Another quirky behavior that I did not understand is also explained by
your post: If you set a textBox to ReadOnly, then the values are
*not* persisted between postbacks. I *know* this behavior changed
between 1.1 and 2.0.
 
G

Guest

Hi Reader,

I forgot to mention about readonly property. Value of any input with
enabled=false or readonly property set to false is not posted with form.
That's why text property disappears between postback when
enableviewstate=false (otherwise is restored from original value that's kept
in textbox's viewstate - the same value is used for - textchanged event). i
don't think they changed it. There's one more thing I didn't explain in my
last post - this it could be the case you're talking about. See code below:

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txt1.Text = "test";
}
}
</script>

<asp:TextBox runat="server" ID="txt1" EnableViewState="false"
ReadOnly="true"/>
<asp:TextBox runat="server" ID="txt2" Text="Test" Readonly="true"
EnableViewState="false"/>
<asp:Button runat="server" ID="btn" Text="Do Postback"/>

Why second text box keeps its text between postbacks? This is because page
compiler sets txt2 to its default value "test" when creating control tree in
page's class constructor/initialisator for incoming request:

private global::System.Web.UI.WebControls.TextBox @__BuildControltxt2() {
global::System.Web.UI.WebControls.TextBox @__ctrl;

#line 22 "C:\Documents and Settings\Mily\My Documents\Visual
Studio 2005\WebSites\TestWebsite\Default.aspx"
@__ctrl = new global::System.Web.UI.WebControls.TextBox();

#line default
#line hidden
this.txt2 = @__ctrl;
@__ctrl.ApplyStyleSheetSkin(this);

#line 22 "C:\Documents and Settings\Mily\My Documents\Visual
Studio 2005\WebSites\TestWebsite\Default.aspx"
@__ctrl.ID = "txt2";

#line default
#line hidden

#line 22 "C:\Documents and Settings\Mily\My Documents\Visual
Studio 2005\WebSites\TestWebsite\Default.aspx"
@__ctrl.Text = "Test";

#line default
#line hidden

#line 22 "C:\Documents and Settings\Mily\My Documents\Visual
Studio 2005\WebSites\TestWebsite\Default.aspx"
@__ctrl.ReadOnly = true;

#line default
#line hidden

#line 22 "C:\Documents and Settings\Mily\My Documents\Visual
Studio 2005\WebSites\TestWebsite\Default.aspx"
@__ctrl.EnableViewState = false;

#line default
#line hidden
return @__ctrl;
}

This method is called regardless of the postback status, so the text
property is always set to a default value taken from aspx control
declaration. Of course if readonly == false Text is overriden by
Request.Form[UniqueID] . I hope everything is clear now :]

Regards

Milosz
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top