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