I am developping an asp.net application. I have a page called "home.aspx" which contains 2 buttons called "button1" and "button2". Each button has a onclick method, respectively called "Button1_click" and "Button2_click". My goal is to share a common variable between these 2 methods, so basically I tried to add a property to the class associated to my page like this :
public partial class Home : Syste.Web.UI.Page
{
private int myproperty=0;
protected void Button1_Click(object sender, EventArgs e)
{
myproperty++;
}
protected void Button2_Click(object sender, EventArgs e)
{
myproperty++;
}
}
With this code, I was thinking that the "private int myproperty=0" would be called only the first time I load and get to the page. And then when I would be on the page, if I click on button1 or button2 the myproperty would just be incrementated by one. But when I am debugging, I see that each time I click on button1 or button2 the "private int myproperty=0" is called again which I don't understand. Is anyone has a solution?
Thanks in advance for your help.
Pageonly exist for the lifetime of the request, each request creates a new instance. If you want to store information between requests you will have to persist it somewhere, if it is just in memory thenSessionis often used, but bear in mind each client will have their own session.