简体   繁体   中英

share a global variable to a class in asp.net application

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.

In ASP.NET if you want use global variables you'll have to use the Session object(if the variable is different for every single user, like count how many time a user click a Button) or Application object(If the variable is common to all the users like count how much user visit your site). Example:

//Set variable
Session["myVariable"] = value
//Get variable
var myVariable = Session["myVariable"]

The syntax is equal for Application just replace Session with Application

Make your variable static

Like this

    public partial class Home : Syste.Web.UI.Page
   {
    private Static int myproperty;

    protected void Button1_Click(object sender, EventArgs e)
    {
        //You can access myproperty here (same copy)
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
         //You can access myproperty here (same copy)
    }
}

You can use Session as below

private int Count
{
  get 
   {
     if (Session["Count"] == null)
     {         Session["Count"] = 0; return 0;      }
     else 
        return (int)Session["Count"] ; 
   }
   set
   {
      Session["Count"] = value;
   }
} 

protected void Page_Load(object sender, EventArgs e)     
{
    if(!IsPostBack)
        Count = 0; 
}

protected void Button1_Click(object sender, EventArgs e)
{
     Count++; 
}

protected void Button2_Click(object sender, EventArgs e)
{
     Count++; 
}

How you want to share the value of the variable myproperty?

-If you want to reuse the value of myproperty per window you need to store the value in the vewstate: http://msdn.microsoft.com/en-us/library/ms227551%28v=vs.85%29.aspx

Another way is to use a asp.net webcontrol hiddenfiled to store the value.

-If you want to reuse the value for session, you can store the value in the session (Session["myproperty"])

-If you want to share the value with all the users you can use the application: http://msdn.microsoft.com/en-us/library/94xkskdf%28v=vs.100%29.aspx

as Ben Robinson said you have to persist that value somewhere. An easy way is to use Session:

  1. Create a "const string" variable in the class to hold the session index like:

    const string MY_PROPERTY = "MyProperty";

  2. then whenever you want to store or read the value use:

    //to store Session[Home.MY_PROPERTY] = int.parse(Session[Home.MY_PROPERTY])+1; //to read int myVariable = int.parse(Session[Home.MY_PROPERTY])

you can also create a property in your class like:

const string MY_PROPERTY = "MyProperty";
public int MyProperty
{
    get
    {
        if (Session[Home.MY_PROPERTY] == null)
        {
            return 0;
        }
        else
        {
            int iValue = 0;
            if (int.TryParse(Session[Home.MY_PROPERTY].ToString(), out iValue))
                return iValue;
            else
                return 0;
        }
    }
    set
    {
        Session[Home.MY_PROPERTY] = value;
    }
}

Only the postback check, before the increment, can resolve the problem. Add following code before increment.

if(!IsPostBack)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM