2013-04-04 17 views
0

我必須形成以音樂的各個流派數組列表,例如我將展示鄉村音樂:ArrayList的會議

public class CountryItemList 
{ 
    ArrayList CountryItems = new ArrayList(); 

    protected void CountryBuy1_Click(object sender, ImageClickEventArgs e) 
    { 
     CountryItems.Add("Johnny Cash - I Walk The Line - $9"); 
    } 
    protected void CountryBuy2_Click(object sender, ImageClickEventArgs e) 
    { 
     CountryItems.Add("Carrie Underwood - Blown Away - $9"); 
    } 
    protected void CountryBuy3_Click(object sender, ImageClickEventArgs e) 
    { 
     CountryItems.Add("Keith Urban - The Story So Far - $9"); 
    } 

    protected void CountryBuy5_Click(object sender, ImageClickEventArgs e) 
    { 
     CountryItems.Add("Taylor Swift - Red - $11"); 
    } 
    protected void CountryBuy6_Click(object sender, ImageClickEventArgs e) 
    { 
     CountryItems.Add("Willie Nelson - Legend - $9"); 
    } 

} 

}

每個購買按鈕被點擊,添加所選專輯到數組列表,從那時起,我希望將數組列表放入一個會話中,並將其帶到「購物車頁面」上的列表框中。我無法取出數組列表並將其轉入會話以轉到下一頁

+1

你正在使用的ArrayList,而不是一般的收藏品之一的任何原因, 對於初學者? – 2013-04-04 19:40:23

+1

您應該將'CountryItems'設爲'List '而不是'ArrayList'。 – Lee 2013-04-04 19:40:39

回答

1

您只需要使用Session []將其結束。

我想使用List而不是ArrayList。

private List<string> _countryItems; 
     public List<string> CountryItems { 
      get { 
       if (_countryItems == null) { 
        _countryItems = (List<string>)Session["CountryItems"]; 
        if (_countryItems == null) { 
         _countryListItems = new List<string>(); 
         Session["CountryItems"] = _countryItems; 
        } 
       } 
       return _countryItems; 
      } 
      set { _countryItems = value; } 
     } 

     protected void CountryBuy6_Click(object sender, ImageClickEventArgs e) { 
      CountryItems.Add("Willie Nelson - Legend - $9"); 
     } 

這樣,你直接從會話引用CountryListItems:像

試試。如果它存在於會話中,您將從會話中獲得值。如果沒有,你會得到一個可以添加值的空列表。這應該在添加元素時提交會話的任何更新。

另外,看看使用CommandEventArgs,它可以讓你創建一個從多個地方調用的單擊事件,然後你就可以發送單獨的評論事件參數,並從標記中獲取值。

0

我不認爲這會獲得最佳購物卡獎,但它會完成這項工作。

它基本上將所選項目存儲到會話中,以便您可以在下一頁中將其恢復。

注意:ArrayList is deprecated,所以請使用通用列表。

enter image description here

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CountryItemList.aspx.cs" 
    Inherits="WebApplication2010.CountryItemList" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:Button runat="server" ID="Button1" OnCommand="Button_Command" 
     CommandName="Johnny Cash - I Walk The Line - $9" 
      Text="Johnny Cash - I Walk The Line - $9" /><br/> 
     <asp:Button runat="server" ID="Button2" OnCommand="Button_Command" 
     CommandName="Carrie Underwood - Blown Away - $9" 
      Text="Carrie Underwood - Blown Away - $9" /><br/> 
     <asp:Button runat="server" ID="Button3" OnCommand="Button_Command" 
     CommandName="Keith Urban - The Story So Far - $9" 
      Text="Keith Urban - The Story So Far - $9" /><br/> 
     <asp:Button runat="server" ID="Button4" OnCommand="Button_Command" 
     CommandName="Taylor Swift - Red - $11" 
      Text="Taylor Swift - Red - $11" /><br/> 
     <asp:Button runat="server" ID="Button5" OnCommand="Button_Command" 
     CommandName="Willie Nelson - Legend - $9" 
      Text="Willie Nelson - Legend - $9" /> 
    </div> 
    </form> 
</body> 
</html> 

public partial class CountryItemList : System.Web.UI.Page 
{ 
    public List<string> CountryItems 
    { 
     get { return Session["CountryItems"] as List<string> ?? new List<string>(); } 
     set { Session["CountryItems"] = value; } 
    } 

    protected void Button_Command(object sender, CommandEventArgs e) 
    { 
     var items = CountryItems; 
     items.Add(e.CommandName); 
     CountryItems = items; 
    } 
} 
1

我一直小於熱衷於使用Session這樣的事情。我發現它不像使用數據庫表一樣可靠,特別是對於像購物車之類的東西。

如果您曾經需要重新啓動您的應用程序池,那麼如果您將其存儲在Session中,則任何當前用戶都將失去其選擇。您也不應該認爲將某些東西放入會話中即可立即使用。

你應該考慮將這些東西存儲在數據庫表中。當他們第一次將東西添加到購物車時,您可以爲每個用戶分配一個GUID,並將每個用戶的產品ID用於每首歌曲。把這個GUID扔到一個cookie中,如果他們以後再回來,他們的購物車仍然會充滿。如果您不想使用Cookie,也可以使用他們的SessionID,如果他們的購物車在關閉窗口時爲空,也可以使用它們。

如果你想繼續使用會話,這樣的事情會工作

protected List<string> SelectedSongs 
    { 
     get 
     { 
      List<string> li = new List<string>(); 

      if (Session["SelectedSongs"] != null) 
      { 
       try 
       { 
        return (List<string>)Session["SelectedSongs"]; 
       } 
       catch (Exception e) 
       { 
        return li; // Something went wrong? Return the empty list. 
       } 
      } 
      else 
      { 
       return li; 
      } 
     } 
     set 
     { 
      Session["SelectedSongs"] = value; 
     } 
    } 

對於將它們添加到列表中,你真的可以只用一個事件處理程序有點清理你的代碼。像這樣的東西

protected void AddToCart(object sender, CommandEventArgs e) 
{ 
    List<string> li = SelectedSongs; 
    li.Add(e.CommandArgument) 
    SelectedSongs = li;  
} 

<asp:Button id="btnAddToCart" Text="Add Song" CommandArgument="Johnny Cash - I Walk The Line - $9" CommandName="AddToCart" runat="server"/> 

<asp:Button id="btnAddToCart" Text="Add Song" CommandArgument="Carrie Underwood - Blown Away - $9" CommandName="AddToCart" runat="server"/> 

等等的其他歌曲。

+0

你是什麼意思:_你也不應該認爲把某些東西放入Session中是可以馬上得到的。 – 2013-04-04 20:57:06

+0

我在li.add(e.CommandArgument) 處得到一個錯誤,並且在添加命令時出現錯誤參數和命令名 – user2246454 2013-04-06 18:41:08

0

試試這個

第1頁:

protected void Page_Load(object sender, EventArgs e) 
{ 
    //Creating Session 
    ArrayList idList = new ArrayList(); 
    idList.Add("1"); 
    idList.Add("2"); 
    idList.Add("3"); 
    Session["idArrayList"] = idList; 
} 

第2頁:

protected void Page_Load(object sender, EventArgs e) 
{ 
    //Retrieving 
    ArrayList idList = (ArrayList)Session["idArrayList"]; 
    string id1 = idList[0].ToString() ; 
    string id2 = idList[1].ToString(); 
    string id3 = idList[2].ToString(); 
} 

來源:http://forums.asp.net/t/1425975.aspx?C+How+to+place+an+arraylist+inside+a+Session+Variable+and+Iterate+through+it