Thursday 15 August 2013

c# - share a global variable to a class in asp.net application -



c# - share a global variable to a class in asp.net application -

i developping asp.net application. have page called "home.aspx" contains 2 buttons called "button1" , "button2". each button has onclick method, respectively called "button1_click" , "button2_click". goal share mutual variable between these 2 methods, tried add together property class associated page :

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 code, thinking "private int myproperty=0" called first time load , page. , when on page, if click on button1 or button2 myproperty incrementated one. when debugging, see each time click on button1 or button2 "private int myproperty=0" called 1 time again don't understand. has solution?

thanks in advance help.

in asp.net if want utilize global variables you'll have utilize session object(if variable different every single user, count how many time user click button) or application object(if variable mutual users count how much user visit site). example:

//set variable session["myvariable"] = value //get variable var myvariable = session["myvariable"]

the syntax equal application replace session application

c# asp.net

No comments:

Post a Comment