C# 单击按钮将字符串添加到列表<;字符串>;

C# 单击按钮将字符串添加到列表<;字符串>;,c#,asp.net,list,C#,Asp.net,List,我想有一个按钮,当点击将采取什么文本框内的文本,并将其添加到字符串列表。然后,我有另一个按钮,单击该按钮时,将列表的计数输出到另一个文本框中。无论我做什么尝试,我总是得到零的计数。有人能帮我做错事吗 c# 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用System.Web; 使用System.Web.UI; 使用System.Web.UI.WebControl; 公共部分类\u默认值:System.Web.UI.Page { List

我想有一个按钮,当点击将采取什么文本框内的文本,并将其添加到字符串列表。然后,我有另一个按钮,单击该按钮时,将列表的计数输出到另一个文本框中。无论我做什么尝试,我总是得到零的计数。有人能帮我做错事吗

c#

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
公共部分类\u默认值:System.Web.UI.Page
{
List itemList=新列表();
受保护的无效页面加载(对象发送方、事件参数e)
{
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
itemList.Add(txtItem.Text);
txtItem.Text=“”;
}
受保护的无效按钮2\u单击(对象发送者,事件参数e)
{
txtItemListCount.Text=itemList.Count.ToString();
}
}
加价

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">


    <asp:TextBox ID="txtItem" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    <br />
    <br />
    <asp:TextBox TextMode="MultiLine" ID="txtItemListCount" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:Button ID="Button2" runat="server" Text="Button 2" 
        onclick="Button2_Click" />


</asp:Content>






每次回发(每次调用服务器时)都会重新创建itemsList。
尝试将列表存储在ViewState或会话中…

项目列表在每次回发时(每次调用服务器时)都会重新创建。 尝试将列表存储在ViewState或会话中…

更改

List<string> itemList = new List<string>();
List itemList=new List();

string itemListKey=“itemListKey”;
列表项列表
{
得到
{
if(会话[itemListKey]==null)
会话[itemListKey]=新列表();
返回(列表)会话[itemListKey];
}
}
更改

List<string> itemList = new List<string>();
List itemList=new List();

string itemListKey=“itemListKey”;
列表项列表
{
得到
{
if(会话[itemListKey]==null)
会话[itemListKey]=新列表();
返回(列表)会话[itemListKey];
}
}

每个请求都会重新创建
System.Web.UI.Page
。因此,
itemList
被初始化为
newlist()单击按钮2时。那么结果计数为零


您需要在请求之间将
itemList
的值存储在一些更持久的资源(例如会话、视图状态、数据库等)中。

每个请求都会重新创建
System.Web.UI.Page
。因此,
itemList
被初始化为
newlist()单击按钮2时。那么结果计数为零


您需要在请求之间将
itemList
的值存储在一些更持久的资源中(例如会话、视图状态、数据库等)。

欢迎使用回发的好先生

每次活动服务器页面上的某些内容需要向服务器发出请求时,都会调用“回发”,刷新页面生命周期的某些部分,例如页面加载

您需要使用Session变量或ViewState变量,Session用于应用程序范围的变量,在许多网站上通常被描述为购物车功能。cart变量执行每个页面请求,直到会话过期。另一方面,ViewState仅在当前页面的生命周期内处于活动状态

在这里,您需要决定将使用哪种类型的容器、会话变量或ViewState变量

向您展示理解的快速解决方法是:

如果会话变量不存在,请创建它

if (Session["myList"] == null)
            Session["myList"] = new List<string>(); 
if(会话[“myList”]==null)
会话[“myList”]=新列表();
将其强制转换为字符串列表以添加项目

List<string> myList = (List<string>)Session["myList"];
        myList.Add(txtItem.Text);
List myList=(List)会话[“myList”];
添加(txtItem.Text);
更新会话变量

Session["myList"] = myList;




  protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["myList"] == null)
            Session["myList"] = new List<string>(); // Check that it exists

        List<string> myList = (List<string>)Session["myList"]; // Cast and add to your list
        myList.Add(txtItem.Text);

        Session["myList"] = myList; // Update the list

        txtItem.Text = "";
    }
  protected void Button2_Click(object sender, EventArgs e)
    {

        if (Session["myList"] == null)
            Session["myList"] = new List<string>();

        List<string> myList = (List<string>)Session["myList"];

        txtItemListCount.Text = myList.Count.ToString();
    }
Session[“myList”]=myList;
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
if(会话[“myList”]==null)
会话[“myList”]=new List();//检查它是否存在
List myList=(List)Session[“myList”];//强制转换并添加到列表中
添加(txtItem.Text);
会话[“myList”]=myList;//更新列表
txtItem.Text=“”;
}
调整按钮2\u单击以加载会话变量

Session["myList"] = myList;




  protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["myList"] == null)
            Session["myList"] = new List<string>(); // Check that it exists

        List<string> myList = (List<string>)Session["myList"]; // Cast and add to your list
        myList.Add(txtItem.Text);

        Session["myList"] = myList; // Update the list

        txtItem.Text = "";
    }
  protected void Button2_Click(object sender, EventArgs e)
    {

        if (Session["myList"] == null)
            Session["myList"] = new List<string>();

        List<string> myList = (List<string>)Session["myList"];

        txtItemListCount.Text = myList.Count.ToString();
    }
protectedvoid按钮2\u单击(对象发送者,事件参数e)
{
if(会话[“myList”]==null)
会话[“myList”]=新列表();
列表myList=(列表)会话[“myList”];
txtItemListCount.Text=myList.Count.ToString();
}

欢迎来到回邮的好先生

每次活动服务器页面上的某些内容需要向服务器发出请求时,都会调用“回发”,刷新页面生命周期的某些部分,例如页面加载

您需要使用Session变量或ViewState变量,Session用于应用程序范围的变量,在许多网站上通常被描述为购物车功能。cart变量执行每个页面请求,直到会话过期。另一方面,ViewState仅在当前页面的生命周期内处于活动状态

在这里,您需要决定将使用哪种类型的容器、会话变量或ViewState变量

向您展示理解的快速解决方法是:

如果会话变量不存在,请创建它

if (Session["myList"] == null)
            Session["myList"] = new List<string>(); 
if(会话[“myList”]==null)
会话[“myList”]=新列表();
将其强制转换为字符串列表以添加项目

List<string> myList = (List<string>)Session["myList"];
        myList.Add(txtItem.Text);
List myList=(List)会话[“myList”];
添加(txtItem.Text);
更新会话变量

Session["myList"] = myList;




  protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["myList"] == null)
            Session["myList"] = new List<string>(); // Check that it exists

        List<string> myList = (List<string>)Session["myList"]; // Cast and add to your list
        myList.Add(txtItem.Text);

        Session["myList"] = myList; // Update the list

        txtItem.Text = "";
    }
  protected void Button2_Click(object sender, EventArgs e)
    {

        if (Session["myList"] == null)
            Session["myList"] = new List<string>();

        List<string> myList = (List<string>)Session["myList"];

        txtItemListCount.Text = myList.Count.ToString();
    }
Session[“myList”]=myList;
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
if(会话[“myList”]==null)
会话[“myList”]=new List();//检查它是否存在
List myList=(List)Session[“myList”];//强制转换并添加到列表中
添加(txtItem.Text);
会话[“myList”]=myList;//更新列表
txtItem.Text=“”;
}
调整按钮2\u单击