2013-05-21 28 views
1

我想要一個按鈕,單擊該按鈕時將文本框內的內容添加到字符串列表中。然後,我有另一個按鈕,單擊時會將列表的計數輸出到另一個文本框中。無論我嘗試什麼,我都繼續計數爲零。有人能幫我解決我做錯了什麼嗎?按鈕單擊以將字符串添加到列表中<string>

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> 

回答

3

變化

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]; 
    } 
} 
+0

我把頭撞在牆上太久了。我想是做小白菜的代價。謝謝! – wootscootinboogie

2

itemsList與每次回發(每個服務器被調用時)創造了一遍。 嘗試存儲在ViewState或會話列表...

+0

是否也能提供填充和閱讀的ViewState或會話代碼的例子嗎? –

+0

糟糕,我沒有看到您的評論。查看Wiktor的答案。 –

1

System.Web.UI.Page對每個請求重新創建。所以當按鈕2被點擊時,itemList被初始化爲new List<string>();。然後結果計數爲零。

您需要在請求之間存儲值itemList,在一些更持久的資源(例如會話,視圖狀態,數據庫等)中。

1

歡迎來到PostBack的好先生!

每次活動服務器頁面上的某些內容需要向服務器發出請求時,會調用「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(); 
    } 
相關問題