ListBox显示System.Collection.ArrayList

时间:2013-04-08 20:35:56

标签: c# asp.net arraylist listbox

我在一个页面上有一个列表框,它将列表框中的项目带到另一个页面上的另一个列表框中,但是当第二个列表框显示“System.Collection.ArrayList”时。

第1页:

protected void btnCheckout_Click(object sender, EventArgs e)
{

    Session["Name"] = NameText.Text;
    Session["PhoneNumber"] = PhoneTextBox.Text;
    Session["Address"] = AddressText.Text;
    Session["Email"] = emailText.Text;
    Session["City"] = cityTextBox.Text;
    ArrayList al = new ArrayList();
    for (int i = 0; i < ItemListBox.Items.Count; i++)
    {
        if (ItemListBox.Items[i].Selected == true)
        {
            al.Add(ItemListBox.Items[i].Value);
        }
    }
    Session["selectedValues"] = al;
    Response.Redirect("Invoice.aspx");

}

2页:

protected void Page_Load(object sender, EventArgs e)
{
    string phoneNumber = (string)(Session["PhoneNumber"]);
    string homeAddress = (string)(Session["Address"]);
    string name = (string)(Session["Name"]);
    string email = (string)(Session["Email"]);
    string city = (string)(Session["City"]);
    phoneLabel.Text = phoneNumber;
    addressLabel.Text = homeAddress;
    nameLabel.Text = name;
    emailLabel.Text = email;
    cityLabel.Text = city;
    invoiceListBox.Items.Add(Session["selectedValues"].ToString());
}

所有其他标签会话都有效,只有列表框不起作用

2 个答案:

答案 0 :(得分:2)

那是因为你将集合的toString()结果(一个字符串)作为单个项目添加。

尝试使用.AddRange

答案 1 :(得分:0)

由于所有对象都存储在Session类型object中,因此您需要先将它们转换回原始类型,然后才能访问其属性。这应该让你知道如何做到这一点。这里的关键是ArrayList al = Session["selectedValues"] as ArrayList;

ArrayList al = Session["selectedValues"] as ArrayList;

if(al != null)
{
    foreach(var item in al)
    {
        invoiceListBox.Items.Add(new ListItem {Value = item.toString(), Text = item.toString()});
    }
}

<强>加 这是一个工作样本。

<强>的Default.aspx

Phone Number: <asp:TextBox ID="txtPhoneNumber" runat="server" />
    <br />
    <asp:ListBox ID="ItemListBox" runat="server" SelectionMode="Multiple">
        <asp:ListItem Value="1">One</asp:ListItem>
        <asp:ListItem Value="2">Two</asp:ListItem>
        <asp:ListItem Value="3">Three</asp:ListItem>
        <asp:ListItem Value="4">Four</asp:ListItem>
    </asp:ListBox>
    <br />
    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
    <br />
    <br />
    <a href="page2.aspx">Page 2</a>

<强> default.aspx.cs

namespace SO15888490
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            Session["PhoneNumber"] = txtPhoneNumber.Text;

            ArrayList al = new ArrayList();

            for (int i = 0; i < ItemListBox.Items.Count; i++)
            {
                if (ItemListBox.Items[i].Selected == true)
                {
                    al.Add(ItemListBox.Items[i].Value);
                }
            }

            Session["ItemsList"] = al;
        }

    }

<强> page2.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Phone Number: <asp:Literal ID="litPhoneNumber" runat="server" />
        <br />
        Selected Items<br />
        <asp:Literal ID="litItemsList" runat="server" />
        <br />
        <a href="Default.aspx">Back to default.aspx</a>
    </div>
    </form>
</body>
</html>
}

<强> page2.aspx.cs

namespace SO15888490 {
    public partial class page2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            litPhoneNumber.Text = (string)Session["PhoneNumber"];

            ArrayList al = Session["ItemsList"] as ArrayList;
            if (al != null)
            {
                foreach (var item in al)
                {
                    litItemsList.Text += item.ToString() + "<br/>";
                }
            }
        }
    } }