Strange behavior of a boolean variable, the value of the variable is lost between the postback

I have a boolean named CheckBoxActivated, which I set to true after checking the username and password.

string name = us.UserName;
string password = us.Password;

if (name.Equals(txtName.Text) && (password.Equals(txtPassword.Text)))
{
    CheckBoxAvtivated = true;

The strange thing is: after I return to the variable, I press another button and immediately becomes “false”, which leads to undesirable behavior.

protected void butNext_Click(object sender, EventArgs e)
{
    if (CheckBoxAvtivated)
    {
        pnlCheckBoxes.Visible = true;
        pnlUserCheckBoxValidation.Visible = false;
    }
    else
    {
        pnlCheckBoxes.Visible = false;
        pnlUserCheckBoxValidation.Visible = true;
    }

Thus, the state of the variable unexpectedly changes to false. Any reason why this could happen?

+5
source share
1 answer

( ) asp.net , viewstate, postbacks. Asp.net http, .

viewstate

ViewState["CheckBoxAvtivated"] = "true";

viewstate

bool CheckBoxAvtivated = bool.Parse(ViewState["CheckBoxAvtivated"].ToString());

, viewstate .

: , . ( - ASP.NET , .) : " ?" , , , . , , , . , . viewstate .

, , , . . , , . , . wikipedia

+19

All Articles