非常简单的问题,但我无法解决它。变量无法在方法之间共享 [英] very simple problem but i am unable resolve it.variable is unable to be shared among methods

查看:52
本文介绍了非常简单的问题,但我无法解决它。变量无法在方法之间共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    List<string> d= new List<string>();
    protected void Button1_Click1(object sender, EventArgs e)
    {
        d.Add(TextBox1.Text);
        d.Add(TextBox2.Text);
        d.Add(TextBox3.Text);
        d.Add(TextBox4.Text);
    }   
    protected void Button2_Click(object sender, EventArgs e)
    {
        d.Add(TextBox5.Text);
        d.Add(TextBox6.Text);
    }
    protected void Button3_Click1(object sender, EventArgs e)
    {
        foreach (string i in d)   //i am unable to get values of textboxes at button3 click.
        {
            Response.Write(i);   //how can i get.
        }
    }
} 

推荐答案

是的,

Amit是对的。



Web技术中的一个基本内容是,在回发请求之后,所有数据都将丢失,并且每个请求都被视为新请求。



因此,每个变量都是原始的单位。



在此代码中,变量List< string> d是定义了所有方法,

因此它将被视为该页面的Golbal变量。



并且与在回发之前变量的旧数据,



将数据保存在viewstate或Session中是个好主意。

如果有的话一个有另一种方式,请在这里发布。



谢谢..



来自,

Dhiraj Luhana
Yes,
Amit is correct.

It is a basic thing in web Technology that between post back request, all the data will
be lost and the each request is considered as a new request.

So, each variable will be in it's original stag.

In this code , the variavle "List<string> d" is defined out side the all the methods,
so it will considered as a Golbal Variable for that page.

And to have the link with the older data for the variable before the post back ,

save the data in the viewstate or in Session is good idea.
If any one has another way , please post it here.

Thanks ..

From,
Dhiraj Luhana


因为, List< string> d 是一个全局变量。继承Web.UI.Page的类中定义的每个变量都将在Page-Lifecycle结束时销毁,因此在Postback中它将为null。您可以将它存储在Session或ViewState中。

尝试这样:

Since, List<string> d is a global variable. Every variable defined in a class inheriting Web.UI.Page will be destroyed at the end of the Page-Lifecycle, hence it will be null in a Postback. You can store it in the Session or the ViewState.
Try like this:
List<string> d= new List<string>();
protected void Button1_Click1(object sender, EventArgs e)
{
    d.Add(TextBox1.Text);
    d.Add(TextBox2.Text);
    d.Add(TextBox3.Text);
    d.Add(TextBox4.Text);
    ViewState["Value"] = d; //Store the value in viewstate
}
protected void Button2_Click(object sender, EventArgs e)
{
    d.Add(TextBox5.Text);
    d.Add(TextBox6.Text);
}
protected void Button3_Click1(object sender, EventArgs e)
{
    List<string> str = ViewState["Value"] as List<string>;
    foreach (string i in str)   //i am unable to get values of textboxes at button3 click.
    {
        Response.Write(i);   //how can i get.
    }
}







- Amit


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        List<string> d = new List<string>();
        d.Add(TextBox1.Text);
        d.Add(TextBox2.Text);
        d.Add(TextBox3.Text);
        d.Add(TextBox4.Text);
        ViewState["d"] = d;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        List<string> d;
        d = (List<string>)ViewState["d"];
        d.Add(TextBox5.Text);
        d.Add(TextBox6.Text);
    }
    protected void Button3_Click1(object sender, EventArgs e)
    {
        List<string> d;
        d = (List<string>)ViewState["d"];
        foreach (string i in d)
        {
            Response.Write(i);
        }
    }
}


这篇关于非常简单的问题,但我无法解决它。变量无法在方法之间共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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