【问题标题】:ArrayList Session数组列表会话
【发布时间】:2014-08-07 15:22:46
【问题描述】:

我为每种音乐类型形成了一个数组列表,例如我将展示乡村音乐:

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

}

}

对于单击的每个购买按钮,所选专辑将添加到数组列表中,从那时起,我希望将数组列表带入会话并将其带到“购物车页面”上的列表框。我在获取数组列表并将其转换为会话以延续到下一页时遇到问题

【问题讨论】:

  • 对于初学者,您有什么理由使用 ArrayList 而不是通用集合之一?
  • 您应该将CountryItems 设为List<string> 而不是ArrayList

标签: c# asp.net arrays session arraylist


【解决方案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");
        }

这样您可以直接从 Session 中引用 CountryListItems。如果它存在于会话中,您将从会话中获取值。如果没有,您将获得一个可以添加值的空列表。然后,这应该会在您添加元素时向会话提交任何更新。

另外,看看使用 CommandEventArgs,这将允许您创建从多个位置调用的单击事件,然后您将能够在 e 上发送单个评论事件 args 并直接从标记中获取值。

【讨论】:

    【解决方案2】:

    我一直不太热衷于将 Session 用于此类事情。我发现它不像使用数据库表那样可靠,尤其是对于像购物车这样的东西。

    如果您需要重新启动应用程序池,如果您将其存储在 Session 中,任何当前用户都会丢失他们的选择。您也不应该假设将某些内容放入 Session 中是立即可用的。

    您应该考虑将这些内容存储在数据库表中。当每个用户第一次向购物车添加东西时,您可以为每个用户分配一个 GUID,并将其与每首歌曲的产品 ID 一起使用。将该 GUID 放入 cookie 中,这样如果他们稍后回来,他们的购物车仍然是满的。如果您不想使用 cookie,也可以使用他们的 SessionID,或者在他们关闭窗口时关心他们的购物车是否为空。

    如果你想继续使用 Session,这样的事情就可以了

        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"/>
    

    其他歌曲以此类推。

    【讨论】:

    • 你的意思是:你也不应该假设把东西放进 Session 是很容易得到的。?
    • 我在 li.add(e.CommandArgument) 处收到错误,在添加命令参数和命令名称时也出现错误
    【解决方案3】:

    我认为这不会获得最佳购物卡奖,但它可以胜任。

    它基本上将选定的项目存储到 Session 中,以便您可以在下一页检索它。

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

    <%@ 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;
        }
    }
    

    【讨论】:

      【解决方案4】:

      试试这个

      第 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

      【讨论】:

        猜你喜欢
        • 2011-09-10
        • 1970-01-01
        • 2011-01-13
        • 2014-07-29
        • 1970-01-01
        • 2016-08-04
        • 2015-03-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多