Why does the label value change?

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

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

But my question is, why does this happen for the label? Why does the label not lose data, even if the ViewState is disabled because the label does not implement 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 is:

 <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 the code behind:

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

I expected to see “Before Clicking” on my shortcut after I clicked the button, but after clicking the button I clicked the “Changed” button on my label.

+5
source share
3 answers

UPD:

I think you misunderstand what ViewState is.

ViewState data is stored at the request of BETWEEN, but not during the page life cycle. BTW - ViewState data is generated after the PreRenderComplete event in the SaveStateComplete event.

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

If you disabled ViewState, just think that it will not be generated on output.

During the page life cycle, all the data assigned to the controls (as well as the fields and properties of the page, since the page is just a class) will be displayed as you defined in aspx. But it will be lost after it is not saved in the ViewState.

0
source

Ok, I deleted my previous answer, I will try to reformulate it with an example.

First, as others have argued, the idea of ​​ViewState is to hold state between postbacks, rather than during a single page load cycle, so what you see is the expected behavior.

To see the difference with the example, try adding your label with two 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 Modified, and btnReset has an empty handler.

Now that EnableViewState set to False , if you click on btn1 , reload the page, btn1_Click will execute, and the page will display with the value = "Changed" label, if you click btnReset page will reload again, but since the view state is disabled, the label will return to source code "Before clicking"

If you set EnableViewState to True on the tab and press btn1 , then btnReset , the label value will remain as “Changed”, as the state is saved during postbacks

Hope that clarifies things a bit

0
source

It will be long and detailed.

Start with this markup. Almost identical to yours, with one extra button and shortcut. I will 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 will use this code. Again, I will explain the purpose of the second handler later:

 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 first load the page. First, ASP.NET reads all the markup, and then goes through the life cycle of the page with all the events. At the parsing stage of the markup, Label1 receives the assigned text Before click , and it never changes later during boot. Thus, a later phase of rendering is what is rendered in HTML and sent to the browser. Thus, Before click displayed on the screen. Nice and easy.

Now we press Button1 . A postback occurs, which is a term that describes a request sent to the same page by one of its controls after it was originally loaded. Again, it all starts with an analysis of ASP.NET markup, and again Label1 gets the assigned text Before click . But then page life cycle events occur that entail control event handlers. And in the handler for Button1 text Label1 changed to Changed . Now here is what is important to note. If ViewState for Label1 was enabled, this new value will be stored there, and so-called tracking will be enabled for the Label1.Text property. But ViewState is disabled, so the new value is not stored anywhere except Label1 . The next step is the rendering page stage, and since Label1.Text still contains the Changed value, this is what is rendered in HTML and sent to the browser for display. However, note that this new value is not sent inside the ViewState field. As you can see, whether ViewState is turned on or does not play any role in what is displayed after this postback.

Now we press Button2 , which will cause another postback. Again, ASP.NET parses the markup, again Label1 gets the text Before click . Then, the ViewState download part is loaded. If ViewState for Label1.Text been enabled, it will load the changed value into this property. But ViewState is disabled, so the value remains unchanged. Thus, when we reach Button2 the click handler, the value of Label1.Text remains Before click , to which Label2.Text assigned. But Label2 has ViewState enabled, and therefore 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 Before click displays.

And to nail it, we will do the third rollback, now again pressing Button1 . Just like during the first postback, Label1 ends with the text Changed . But what about Label2 ? Well, this one has a ViewState enabled, so during the initial parsing of the markup, ASP.NET assigns it a Blah value, and at the time of loading ViewState it replaces this value with Before click . Nothing else affects this value during the life cycle of the page (we did not press the Button2 button this time), and therefore we finally see Changed and Before click in the browser.

I hope this makes it clear what ViewState is for and what it disables. If you want to delve deeper into the work of ViewState, I would highly recommend this article: TRULY Understanding ViewState

0
source

Source: https://habr.com/ru/post/1259539/


All Articles