Reputation: 8291
In this post, Understanding ASP.NET View State, the author says this:
It is a common misconception among developers that view state is somehow responsible for having TextBoxes, CheckBoxes, DropDownLists, and other Web controls remember their values across postback. This is not the case, as the values are identified via posted back form field values, and assigned in the LoadPostData() method for those controls that implement IPostBackDataHandler.
So, when I disable view state for a TextBox, it still persists its text value across postback, which is correct based on the description above.
However, when I disable view state for a ListBox, which also implements IPostBackDataHandler, it does not persists its state in postbacks. For example, the code provided below is supposed to add duplicate items when a button (in the same webform) is clicked (with an empty event handler), but it does not.
Am I missing something here?
protected void Page_Load(object sender, EventArgs e)
{
lbox.Items.Add("1");
lbox.Items.Add("2");
lbox.Items.Add("3");
}
Upvotes: 1
Views: 217
Reputation: 55489
In the sentences you quote, the word "values" refers specifically to the form field values posted by the browser to the server when the user submits the form. These values are defined by the HTML specification:
<input type="text">
element, the browser posts the text entered in the text box. The TextBox control's IPostBackDataHandler implementation reads this value and assigns it to the Text property.<select>
element, the browser posts the value
of each selected <option>
. (The browser does not post the entire list of <option>
elements.) The ListBox control's IPostBackDataHandler implementation reads these values and selects/deselects each ListItem accordingly. (The implementation does not add any items.)The important point is that the browser posts these values regardless of whether view state is enabled or disabled. Thus, even when view state is disabled, TextBox.Text
and ListBox.SelectedValue
will retain the user's input across postbacks.
However, anything else not normally posted by the browser (such as the list of options in a ListBox) requires view state to be enabled for it to be preserved across postbacks.
Upvotes: 1
Reputation: 894
I think the answer can be found from the image below. (And as tested)
As you notice in Step 1, the value in lblMessage.Text is "Hello World!", without anything to Raise PostBack Event Stage, therefore the value is retained as is.
<asp:Label runat="server" ID="lblMessage" Font-Name="Verdana" Text="Hello, World!"></asp:Label> <br /> <asp:Button runat="server" Text="Change Message" ID="btnSubmit"></asp:Button> <br /> <asp:Button runat="server" Text="Empty Postback"></asp:Button>
And the code-behind class contains the following event handler for the Button's Click event:
private void btnSubmit_Click(object sender, EventArgs e) { lblMessage.Text = "Goodbye, Everyone!"; }
Next then, for textboxes EVEN if you disable the view-state to a specific control / whole page, what's saved is the PostBack Event, thats why if you take a look at Step 3, the previous PostBack is loaded as part of Load View State Stage, which makes the "Hello World!" that's been Instantiated, overwritten.
This explanation BTW only applies for control events that does not use DataSource, other controls that requires DataSource seems implicitly defined in the doc.
Upvotes: 2