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.

  • 1
    What do you call "data"? Text on the label and on the textbox? If so, how is this text set, in the markup? – Andrei Nov 10 '16 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. –  Nov 10 '16 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"? – Andrei Nov 10 '16 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? –  Nov 10 '16 at 11:55
  • What text you are getting on label? – Imad Nov 10 '16 at 12:02
  • @user5032790, that sounds right. I mean the behavior you should see is that when you change Label's text on click, and then trigger another postback, if view state on label is disabled the changed text should be gone. Is it not what you see? Can you post a complete code of your use case? – Andrei Nov 10 '16 at 12:08
  • @Andrei Yes. The changed text should be gone as you said. But the changed text is not gone and I see the changed text even by disabling Viewstate. –  Nov 10 '16 at 12:29
  • @user5032790. I see, that's not expected. Please post your code: markup, the way you assign the text, the way you check on next postback – Andrei Nov 10 '16 at 12:33
  • @user5032790, i see updated code. But again, how do you check on next postback? From your update it seems you check by looking at the page - you will see the changed value and it is expected. If you were to trigger another postback though, debugging through the code behind you would see the value gone – Andrei Nov 10 '16 at 12:36
  • @Andrei Isn't the button click does the postback? Why I should use another postback when I already have one (button_click)? And why it doesn't update the label's text on the page. I appreciate yur help if you provide an answer and explain it. tnx. –  Nov 10 '16 at 12:41
  • @user5032790, i'll do an answer with details in an hour or so, watch out – Andrei Nov 10 '16 at 12:53
  • You seem to be asking why the label text is updated on postback but you are setting that label in your click event. What do you expect that click event handler to do if not to update the value of that label? – Chris Nov 10 '16 at 12:55
  • @Chris Of course it should updates the value of that label after click event but then it should lost the updated value because I disabled the ViewState for label. –  Nov 10 '16 at 13:00
  • Viewstate is used for transferring state between requests. Since the label text is being set in this request it doesn't matter what its previous state was so the fact you aren't remembering it is irrelevant. Try adding a second button with a click handler that doesn't set the label text. If you click button 1 (and update the text) and then click button 2 at that point I would expect it to "forget" the state of the label and put it back to its default. – Chris Nov 10 '16 at 13:03
  • @Andrei I think I understand what you said. Just please tell me am I right? 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? –  Nov 10 '16 at 14:02
  • @Andrei And also after I changed the text the result that rendered in browser called Response or what? I think I just didn't know when exactly the postback occurs? And please tell me when? –  Nov 10 '16 at 14:03
  • @user5032790, summed it up in an answer below. Sorry for the amount of text in it, I tried to explain as clear as possible everything that happens on the page – Andrei Nov 10 '16 at 14:26

3 Answers3

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Alex Lebedev
  • 601
  • 5
  • 14
  • Thanks for your answer. But my question is why EnableViewState=false is not working for label. –  Nov 10 '16 at 12:44
  • @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 Nov 10 '16 at 12:47
  • @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? –  Nov 10 '16 at 12:52
  • @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. – Chris Nov 10 '16 at 12:57
  • 1
    @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. – Maarten Nov 10 '16 at 12:58
  • So, if you **after that** perform another postback, **then** that value will not be kept after the postback. – Maarten Nov 10 '16 at 12:58
  • Example with postback **enabled**: request -> clientside page with label empty -> postback -> eventhandler executes and set label -> clientside page with **filled label** -> another postback -> different eventhandler executes which does nothing with the label -> clientside page with **filled label** (since postback is **enabled**). – Maarten Nov 10 '16 at 13:01
  • Example with postback **disabled**: request -> clientside page with label empty -> postback -> eventhandler executes and set label -> clientside page with **filled label** -> another postback -> different eventhandler executes which does nothing with the label -> clientside page with **empty label** (since postback is **disabled**). – Maarten Nov 10 '16 at 13:02
  • @Maarten Please provide an answer in more details with that comment that I upvoted it. I think it should be what I missed. Thanks. –  Nov 10 '16 at 13:17
  • @Maarten I think I understand what you said. Just please tell me am I right? 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? –  Nov 10 '16 at 13:49
  • @Maarten And also after I changed the text the result that rendered in browser called **Response** or what? –  Nov 10 '16 at 13:51
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

KMoussa
  • 1,568
  • 7
  • 11
  • 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? –  Nov 10 '16 at 13:48
  • And also after I changed the text the result that rendered in browser called **Response** or what? –  Nov 10 '16 at 13:52
  • 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 http://stackoverflow.com/questions/8457297/asp-net-page-life-cycle-explanation if you're not familiar with the ASP.NET page life cycle – KMoussa Nov 10 '16 at 14:02
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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Andrei
  • 55,890
  • 9
  • 87
  • 108