In “edmx” page I have button control with event “NextButton_Click” for click. When I click this button the variables “index” doesn’t want to change to “40” and the “text”
variable doesn’t want to change to “active”. These variables are always in the same state “text” is always equal to “start” and “index” is always equal to “10”. Why they don’t want to change with (index = 40;
text = “active”;) as I wrote in the click button event method ?
public partial class CountriesTowns : System.Web.UI.Page
{
int index = 10;
string text = "start";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
index = 20;
text = "stop";
}
}
//click next button
protected void NextButton_Click(object sender, EventArgs e)
{
Response.Write(index);
Response.Write(text);
index = 40;
text = "active";
}
HTTP is stateless, every object like your
indexortext(and even all controls) are destroyed at the end of the page’s life-cycle. So they are always initialized with their default value.You can use a control(f.e. a
TextBoxor aHiddenField) to persist their value across postbacks.But there are other options:
Nine Options for Managing Persistent User State in Your ASP.NET Application