c# - Button click to add string to List<string> -
i have button when clicked take text of what's inside text box , add list of strings. then, have button when clicked output count of list text box. keep on getting count of zero, no matter try. can me i'm doing wrong?
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(); } } markup
<%@ 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>
change
list<string> itemlist = new list<string>(); to
string itemlistkey = "itemlistkey"; list<string> itemlist { { if ( session[itemlistkey] == null ) session[itemlistkey] = new list<string>(); return (list<string>)session[itemlistkey]; } }
Comments
Post a Comment