将值存储在变量中 [英] store values in variable

查看:60
本文介绍了将值存储在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

如何存储Button_Click事件下的变量的值,以便可以将其再次用于另一个Button_Click事件?

Hello all,

How can I store the value of the variables which are under Button_Click event, so that I can use that value again for another Button_Click event ?

推荐答案

这取决于环境你在.
WinForms的过程与网站的过程非常不同.

对于WinForms,您需要做的就是将定义移到类级别,因此它不在click事件处理程序之内:
That depends on the environment you are in.
The procedure for WinForms is very different from the procedure for a web site.

For WinForms, all you need to do is move teh definition to class level, so it is not within the click event handler:
private int myInt = 6;
private void myButton_Click(object sender, EventArgs e)
    {
    myInt++;
    }



对于Web来说,它有点复杂,因为在将页面加载到客户端之后不维护类变量(并且它们将在回发之前全部重置).在这种情况下,请使用会话:



For Web, it is a bit more complex, because class variables are not maintained after a page is loaded to the client (and they will all be reset before a post back). In this case, use the Session:

   int myInt = Session["MyInt"];
...
   myInt++;
...
   Session["MyInt"] = myInt;


   // one button click
Session["op1"] = op1;
Session["op2"] = op2;
Session["op"] = op;

//another button click event
op1 = int.Parse(Session["op1"].ToString());
            op2 = int.Parse(Session["op2"].ToString());
            op = Session["op"].ToString();


这篇关于将值存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆