5

I know even if the ViewState is disabled for the TextBox, we are not losing the data because it implements the IPostBackDataHandler interface.

<asp:TextBox ID="TextBox1" runat="server" EnableViewState="False"/> 

But my question is why this happens for label too? Why label is not losing it's data even if the ViewState is disabled since the label doesn't implements the IPostBackDataHandler interface?

<asp:Label ID="Label1" runat="server" EnableViewState="False" ViewStateMode="Disabled"/> 

TextBox definition:

public class TextBox : WebControl, IPostBackDataHandler, 

Label definition:

public class Label : WebControl, ITextControl 

My code:

<form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" EnableViewState="False" ViewStateMode="Disabled" Text="Before click"></asp:Label> <asp:TextBox ID="TextBox1" runat="server" EnableViewState="False"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_OnClick"/> </div> </form> 

And code behind:

protected void Button1_OnClick(object sender, EventArgs e) { Label1.Text = "Changed."; } 

I expected to see the "Before click" in my label after I clicked the button but I see the "Changed" text in my label after I clicked the button.

17
  • 1
    What do you call "data"? Text on the label and on the textbox? If so, how is this text set, in the markup? Commented Nov 10, 2016 at 11:45
  • @Andrei On the button click I write this TextBox1.Text = "Changed"; Label1.Text = "Changed."; and I expect the label loses it's data because I disable viewstate and it doesn't implement IPostBackDataHandler. Commented Nov 10, 2016 at 11:47
  • Ok, it is clear what you mean by data but not what you mean by "loses". So you change text on click. Now what do you expect? Page loads and the control has value other than "Changed"? Or next postback occurs and control has value other than "Changed"? Commented Nov 10, 2016 at 11:52
  • @Andrei What is the usage of EnableViewState="False"? Shouldn't by disabling it I lose the changed data after postbacks? It's clear why EnableViewState="False" is not working for Textbox but I'm wondering why it's not working for label too? Commented Nov 10, 2016 at 11:55
  • What text you are getting on label? Commented Nov 10, 2016 at 12:02

3 Answers 3

0

Ok I deleted my previous answer, I will try to re-state it with an example.

First, as others stated, the idea of ViewState is to hold the state between postbacks, rather than during a single page load cycle, so what you're seeing is the expected behavior.

To see the difference with an example, try adding your label with 2 buttons:

 <asp:Label ID="Label1" runat="server" EnableViewState="False" Text="Before click"></asp:Label> <asp:Button ID="btn1" Text="Click" OnClick="btn1_Click" runat="server" /> <asp:Button ID="btnReset" Text="Reset" OnClick="btnReset_Click" runat="server" /> 

Where btn1 sets the value to "Changed", and btnReset has an empty handler.

Now with EnableViewState set to False, if you click on btn1 the page reloads, btn1_Click is executed, and the page is rendered with the label value = "Changed", if you click btnReset the page will reload again, but since view state is disabled, the label will revert to its original text "Before click"

If you set EnableViewState to True on the lable and click btn1 then btnReset, the label value will stay as "Changed", since the state is kept during postbacks

Hope that clarifies things a bit

Sign up to request clarification or add additional context in comments.

3 Comments

So are you trying to say that setting the value of the label after clicking the btn1 and inside btn1_Click is after the postback occurs? Right?
And also after I changed the text the result that rendered in browser called Response or what?
the button handler is executed during postback .. what rendered in the browser is part of the HTTP response sent by the server yes .. you might want to have a look at the answers here stackoverflow.com/questions/8457297/… if you're not familiar with the ASP.NET page life cycle
0

This is going to be long and detailed.

Let's start with this markup. Almost identical to yours, with one additional button and a label. I'll explain why we need them later.

<form id="form1" runat="server"> <div> <asp:Label ID="Label1" runat="server" EnableViewState="False" ViewStateMode="Disabled" Text="Before click"></asp:Label> <asp:TextBox ID="TextBox1" runat="server" EnableViewState="False"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_OnClick"/> <asp:Label ID="Label2" runat="server" Text="Blah" /> <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_OnClick"/> </div> </form> 

And we'll use this code behind. Again, I'll explain the purpose of the second handler later on:

protected void Button1_OnClick(object sender, EventArgs e) { Label1.Text = "Changed"; } protected void Button2_OnClick(object sender, EventArgs e) { Label2.Text = Label1.Text; } 

Let's see what happens when we initially load the page. First ASP.NET reads all the markup, and then goes through the page life cycle with all the events. In markup parsing stage Label1 gets assigned text Before click, and it is never changed later during initial load. So later during rendering phase this is what is getting rendered into HTML and sent to browser. Thus Before click is displayed on the screen. Nice and easy.

Now we click Button1. Postback occurs, which is a term that describes a request sent to the same page by one of its controls after it was initially loaded. Again, everything starts with ASP.NET parsing the markup, and again Label1 gets assigned text Before click. But then page life cycle events happen, including control event handlers. And in the handler for Button1 the text of Label1 is changed to Changed. Now here is the important thing to note: if ViewState for the Label1 was enabled, this new value would be stored in there, and so called tracking would be enabled for property Label1.Text. But ViewState is disabled, and so the new value is not stored anywhere except Label1. Next comes the rendering page stage, and since Label1.Text still holds the value Changed this is what is rendered into HTML and sent to the browser to display. Note however that this new value is not sent inside the ViewState field. As you can see, whether ViewState is enabled or not plays no role in what is displayed after this postback.

Now we'll click Button2, which would trigger another postback. Again, ASP.NET parses the markup, again Label1 gets text Before click. Then comes ViewState loading part. If ViewState for Label1.Text was enabled, it would load changed value into this property. But ViewState is disabled, and so the value stays the same. Thus, when we reach Button2 click handler, the value of Label1.Text is still Before click, which is assigned to Label2.Text. But Label2 has ViewState enabled, and so this new value for its text is stored in ViewState and sent to the client (ViewState is implemented as a hidden field on the client side). When everything gets to the browser, we can see Label1 and Label2 both displaying Before click.

And to nail it down we'll do a third postback, now clicking Button1 again. Just as during the first postback, Label1 ends up with Changed text. But what about Label2? Well, this one has ViewState enabled, so during initial markup parsing ASP.NET assigns it the value Blah, and during ViewState loading it replaces this value with Before click. Nothing else affects this value during the page life cycle (we did not click Button2 this time), and so we end up seeing Changed and Before click in the browser.

Hopefully it makes it clear what the ViewState is for and what disabling it does. If you want to dive even deeper into how ViewState works, I would greatly recommend this article: TRULY Understanding ViewState.

Comments

0

I think you have wrong understanding what ViewState is.

Data in ViewState is being stored BETWEEN requests, but not DURING page lifecycle. BTW - ViewState data is generated after PreRenderComplete event in SaveStateComplete event.

https://msdn.microsoft.com/en-us/library/ms178472.aspx

If you have ViewState switched off - just think that it will not be generated in output.

During page lifecycle all data assigned to controls(and also to page fields and properties, as the page is just a class) will be rendered as you defined in aspx. But will be lost after, unless is saved in ViewState.

11 Comments

Thanks for your answer. But my question is why EnableViewState=false is not working for label.
@user5032790 The label is working as expected, your expections are wrong. Do you know exactly what it is that the View-state does, and what exactly doesn't work when you disable it?
@Maarten When I change the Label's text on click, it then trigger postback and since I disabled the viewstate on label the changed text should be gone. Isn't it the expected behavior? Also please tell me isn't the button click does the postback?
@user5032790: the label's text is changed by the postback. It doesn't change the text and then postback. The click happens server side so the course of events is roughly 1) You click the button 2) browser makes request to server 3) server processes request including the button click. It is here the label gets changed. 4) Server sends new page back to browser including your updated label value.
@user5032790 The viewstate enables controls to save their current state during a postback. This means that if you have a label with a value which was set in code, and a postback is done, then that value-from-code will be kept during a postback. If you disable viewstate, that value is not kept. Now, to your example. If you set the value of the label in a eventhandler which is executed during a postback, then of course that value is displayed, since that value doesn't have anything to do with a postback.
|