I have a boolean variable called CheckBoxActivated that I assign to true after validating a user name 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 assigning ‘true’ to the variable I click another button and immediately it becomes ‘false’ which results in undesired behaviour.
protected void butNext_Click(object sender, EventArgs e)
{
if (CheckBoxAvtivated)
{
pnlCheckBoxes.Visible = true;
pnlUserCheckBoxValidation.Visible = false;
}
else
{
pnlCheckBoxes.Visible = false;
pnlUserCheckBoxValidation.Visible = true;
}
The state of the variable thus changes to false unexpectedly. Any reason why this could happen?
The class level variables (global variables) in asp.net does not maintain state between postbacks you have to use viewstate if you want to keep the state between postbacks. ASP.NET is based on the HTTP protocol, which is stateless protocol and provides no means of storing a user’s data between requests
To set in viewstate
To get from viewstate
It is important to learn where to use viewstate and where it should not be used.
The Role of View State
From MSDN.
Stateless protocol
From Wikipedia