简体   繁体   English

与asp.net应用程序中的类共享一个全局变量

[英]share a global variable to a class in asp.net application

I am developping an asp.net application. 我正在开发一个asp.net应用程序。 I have a page called "home.aspx" which contains 2 buttons called "button1" and "button2". 我有一个名为“ home.aspx”的页面,其中包含2个名为“ button1”和“ button2”的按钮。 Each button has a onclick method, respectively called "Button1_click" and "Button2_click". 每个按钮都有一个onclick方法,分别称为“ Button1_click”和“ 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. 有了这段代码,我一直认为只有在我第一次加载并进入页面时才会调用“ private int myproperty = 0”。 And then when I would be on the page, if I click on button1 or button2 the myproperty would just be incrementated by one. 然后,当我进入页面时,如果我单击button1或button2,则myproperty只会增加一个。 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. 但是在调试时,我发现每次单击button1或button2时,都会再次调用“ private int myproperty = 0”,这是我不理解的。 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). 在ASP.NET中,如果要使用全局变量,则必须使用Session对象(如果每个用户的变量都不相同,例如计算用户单击按钮的次数)或Application对象(如果变量是通用的)来统计所有用户的访问量)。 Example: 例:

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

The syntax is equal for Application just replace Session with Application 语法是相等的Application只需更换SessionApplication

Make your variable static 使变量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 您可以如下使用Session

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? 您想如何共享变量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 -如果要重用每个窗口的myproperty值, myproperty需要将值存储在vewstate中: http : //msdn.microsoft.com/zh-cn/library/ms227551%28v=vs.85%29.aspx

Another way is to use a asp.net webcontrol hiddenfiled to store the value. 另一种方法是使用asp.net Web控件hiddenfiled存储值。

-If you want to reuse the value for session, you can store the value in the session (Session["myproperty"]) -如果您想将值重复用于会话,则可以将值存储在会话中(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 -如果您想与所有用户共享价值,则可以使用该应用程序: http : //msdn.microsoft.com/zh-cn/library/94xkskdf%28v=vs.100%29.aspx

as Ben Robinson said you have to persist that value somewhere. 正如本·罗宾逊(Ben Robinson)所说,您必须在某个地方坚持这一价值观。 An easy way is to use Session: 一种简单的方法是使用Session:

  1. Create a "const string" variable in the class to hold the session index like: 在类中创建一个“常量字符串”变量以保存会话索引,例如:

    const string MY_PROPERTY = "MyProperty"; 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; //存储Session [Home.MY_PROPERTY] = int.parse(Session [Home.MY_PROPERTY])+ 1; //to read int myVariable = int.parse(Session[Home.MY_PROPERTY]) //读取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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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