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
"GroupReader" wrote:
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.