静态变量或静态列表问题? [英] Static variable or static list problem ?

查看:67
本文介绍了静态变量或静态列表问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我在我的asp.net中使用静态字符串列表(C#)代码如

Hello ,
I am using static list of string in my asp.net(C#) Code like

private static List<string> newlist = new List<string>();



现在我有一个文本框,我从中接受输入字符串并在按钮上单击添加到列表中


Now I have one textbox from which i am taking input string and adding in to list on button click like

protected void Button1_Click(object sender, EventArgs e)
   {
       newlist.Add(TextBox1.Text);
       TextBox1.Text = "";

   }



在另一个按钮上点击我在文字控件上显示这个列表,使用这样的foreach循环,


And on another button click i am showing this list on literal control using foreach loop like this,

protected void Button2_Click(object sender, EventArgs e)
    {
        ltrList.Text = "";
        foreach(var l in newlist)
        {
            ltrList.Text += l.ToString() + "<br/>";
        }
    }





我在服务器上托管此页面。当用户一次访问此webform并在列表中添加项目,从文本框中输入项目时,项目将显示给两个用户。

假设一个用户添加Veeshal并添加到列表中,另一个用户添加Amol并添加到列表中。当他们点击show时,两个用户都将获得列表Veeshal和Amol中的所有项目。

我认为这是因为静态。如何解决这个问题呢。我不想彼此显示物品清单。



我尝试过的事情:



我试过

protected void Button2_Click(object sender,EventArgs e)

{

ltrList.Text =;

foreach(varl in newlist)

{

ltrList.Text + = l.ToString()+
;

}

}



I have host this page on server. When at a time to users access this webform and add items in list giving input from textbox the items show to eachother to both users.
Suppose one user add "Veeshal" and add to list and another user add "Amol" and add to list. And when they click on show, both users will get all items from list "Veeshal" and "Amol".
I think it's because of static. How to solve this problem. I don't want to show the list of items to each other.

What I have tried:

I have tried
protected void Button2_Click(object sender, EventArgs e)
{
ltrList.Text = "";
foreach(var l in newlist)
{
ltrList.Text += l.ToString() + "
";
}
}

推荐答案

简单:不要使用静态

问题是 static 变量可用于应用程序中所有类的实例 - 一个网站可以(但可能不是)许多用户会话。

而是将您的列表存储在会话中 - 在您的Page_Load中检查它是否存在,如果存在则检索它。如果没有,则创建它。无论哪种方式,你将它存储为类级实例变量,而不是静态

当你完成向它添加项目时,你把它放回去下次会议。
Simple: don't use static.
The problem is that static variables are available to all instances of a class in an application - which for a web site can be (but may not be) a number of user sessions.
Instead, store your list in the Session - in your Page_Load you check if it exists, and retrieve it if it does. If it doesn't, you then create it. Either way, you store it as a class level instance variable, not static.
When you finish adding items to it, you put it back into the Session for next time.


除了上述解决方案,



你应该用更简单的方式使用隐藏字段



在aspx中创建一个隐藏的字段控件

In addition to above solution,

you shall do this in a simpler way using hidden field

create a hidden field control in aspx
<asp:HiddenField ID="hdnfldList" runat="server" />



,您应将文本框中的文本追加到文学作品中l控制




and you shall append the text from textbox to the literal control

protected void Button1_Click(object sender, EventArgs e)
       {
            ltrList.Text += TextBox1.Text + "<br/>";
            TextBox1.Text = "";

       }


这篇关于静态变量或静态列表问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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