473,509 Members | 12,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

viewstate

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
Jan 29 '07 #1
5 2999
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
--
Milosz
"Just Me" wrote:
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
Jan 29 '07 #2
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.

Jan 29 '07 #3
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
--
Milosz
"GroupReader" wrote:
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.

Jan 29 '07 #4
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.

Jan 30 '07 #5
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.

Jan 31 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
21628
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
3
2629
by: Steve Drake | last post by:
All, I have a CONTROL that contains 1 control (Control ONE), the 1 control that it can contain 1 or 2 control (Control A and B). Control A, raises and event and Control ONE receives this event...
10
2241
by: neo | last post by:
hi, I am studying ASP.NET and have few questions - 1) The session ID and values of controls is stored in VIEWSTATE variable. So now when we put EnableViewState="false" in Page directive and...
1
1758
by: Simon | last post by:
Hi everyone, I have a quick question that I hope someone can help me with: I've made a user control that contains a text box and some validation functionality. This control has a few extra...
7
2029
by: et | last post by:
I'm not sure I understand the use of the ViewState. Do I understand correctly that values of controls are automatically held in a hidden control called ViewState? If so, then why can't we get...
3
4548
by: RCS | last post by:
I have an app that I have different "sections" that I want to switch back and forth from, all while having the server maintain viewstate for each page. In other words, when I am on Page1.aspx and...
9
1852
by: Mark Broadbent | last post by:
Been a while since I've touched asp.net but one thing that always seems to fustrate me is the loss of state on variable declarations. Is there anyway (i.e. assigning an attribute etc) to instruct...
6
2058
by: hitendra15 | last post by:
Hi I have created web user control which has Repeater control and Linkbutton in ItemTemplate of repeater control, following is the code for this control On first load it runs fine but when...
6
4444
by: paul.hester | last post by:
Hi all, Does anyone know why the ViewState would be empty? When I'm receiving a postback, I can access a posted value using controlName.Value but not ViewState. I have EnableViewState set...
12
1897
by: Nick C | last post by:
Hi How can i reduce the viewstate for my asp.net application. It is getting very large now. What is a good solution? thanks N
0
7137
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7417
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.