Arbejdsglæde
Arbejdsglæde

Reputation: 14088

Why check box is checked after postback

I have next code,

<form id="form1" runat="server">
<asp:Label runat="server" ID="Label1" EnableViewState="false" />
<asp:CheckBox runat="server" ID="Check1" EnableViewState="false" Checked="false" />
<asp:Button runat="server" ID="Button1" Text="Button1" />
</form>

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack) return;

    Label1.Text = "Label value";
    Check1.Checked = true;
}

Why on postback text is despairing but check box still checked ? Thanks

Upvotes: 0

Views: 849

Answers (2)

KV Prajapati
KV Prajapati

Reputation: 94645

Citing references from this MSDN article - Understanding ASP.NET View State by Scott Mitchell doc:

Page Life cycle Stage 3 - Load Postback Data

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.

Upvotes: 2

Guffa
Guffa

Reputation: 700362

Because the checkbox picks up the state from the form data, so that it remains checked when it is recreated.

The viewstate is only needed to remember what state the checkbox had before, so that the server side Change event works.

Upvotes: 1

Related Questions