【问题标题】:Button click to add string to List<string>按钮单击以将字符串添加到 List<string>
【发布时间】:2013-05-21 23:30:21
【问题描述】:

我想要一个按钮,当单击该按钮时,它将获取文本框内的文本并将其添加到字符串列表中。然后,我有另一个按钮,单击该按钮会将列表的计数输出到另一个文本框中。无论我尝试什么,我都会继续计数为零。有人可以帮我解决我做错了什么吗?

c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    List<string> itemList = new List<string>();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        itemList.Add(txtItem.Text);
        txtItem.Text = "";
    }
    protected void Button2_Click(object sender, EventArgs 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>

【问题讨论】:

    标签: c# asp.net list


    【解决方案1】:

    改变

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

    string itemListKey = "itemListKey";
    List<string> itemList
    {
      get
      {
         if ( Session[itemListKey] == null )
            Session[itemListKey] = new List<string>();
         return (List<string>)Session[itemListKey];
      }
    }
    

    【讨论】:

    • 我的头撞墙太久了。我猜,作为菜鸟的代价。谢谢!
    【解决方案2】:

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

    【讨论】:

    • 您能否提供有关填充和读取 ViewState 或 Session 的代码示例?
    • 糟糕,我没有看到您的评论。查看 Wiktor 的答案。
    【解决方案3】:

    System.Web.UI.Page 会在每个请求上重新创建。因此,当单击 button2 时,itemList 被初始化为 new List&lt;string&gt;();。然后结果计数为零。

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

    【讨论】:

      【解决方案4】:

      欢迎来到 PostBack 的好先生!

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

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

      这是您需要决定将使用哪种类型的容器、Session 变量或 ViewState 变量的地方。

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

      如果 Session 变量不存在,则创建它。

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

      将其转换为字符串列表以添加您的项目。

      List<string> myList = (List<string>)Session["myList"];
              myList.Add(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 = "";
          }
      

      调整 button2_click 以加载会话变量

        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();
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-04
        • 2012-01-28
        • 2012-09-14
        • 2021-07-31
        • 2012-12-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多