Pass ListBox values from one page to another in ASP.Net

Last Reply on Mar 05, 2014 12:07 AM By Azim

Posted on Mar 04, 2014 11:28 PM

Hello all

I am trying to add the total of the items in my listbox

i have done this with my checkboxlist and it works no problem. When i try to add the total of my products in listbox i get 0.

 

<div id="available">
            <asp:ListBox runat="server" ID="AvailableProducts" Height="500px" Width="300px" >
            </asp:ListBox>            <br />
            <asp:Button runat="server" ID="addProduct" Text=">>" Width="75px" OnClick="addProduct_Click1" />
        </div>

 

protected void Page_PreInit(object sender, EventArgs e)    {
        List<Product> products = CreateData.MakeProducts();
        foreach (Product p in products)        {
            AvailableProducts.Items.Add(new ListItem(p.Name, Convert.ToString(p.Price)));
        }    }    protected void addProduct_Click1(object sender, EventArgs e)
    {        double total = 0;
        while (AvailableProducts.Items.Count > 0 && AvailableProducts.SelectedItem != null)
        {            ListItem li = AvailableProducts.SelectedItem;
            li.Selected = false;            ShoppingCart.Items.Add(li);
            AvailableProducts.Items.Remove(li);        }
        foreach (ListItem i in ShoppingCart.Items)        {
            if (i.Selected)            {
                total += double.Parse(i.Value);            }        }
        lbltotal.Text = total.ToString();    }

Another thing i am trying to do with this project is.

create a arraylist or list of product names and send them them to my exit.aspx so i can show the selected products at the exit page.

I used a Response.Redirect() to make a variable of customer status and pass it page to page I have it working for only the first page

Posted on Mar 05, 2014 12:07 AM Modified on on Mar 19, 2014 05:51 AM

You can keep the ArrayList in Session and get the data on second page.
Page1:

<form id="form2" runat="server">
<div>
    <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
        <asp:ListItem Text="Mumbai" />
        <asp:ListItem Text="Delhi" />
        <asp:ListItem Text="London" />
        <asp:ListItem Text="Germany" />
        <asp:ListItem Text="Dhaka" />
        <asp:ListItem Text="Sydney" />
    </asp:ListBox>
    <br />
    <asp:Button ID="Button1" runat="server" OnClick="PassSelected" Text="Pass Selected" />
</div>
</form>

 

protected void PassSelected(object sender, EventArgs e)
{
    ArrayList cities = new ArrayList();
    foreach (ListItem item in this.ListBox1.Items)
    {
        if (item.Selected)
        {
            cities.Add(item.Text);
        }
    }
    Session["Cities"] = cities;
    Response.Redirect("Page2.aspx");
}

Page2:

<form id="form2" runat="server">
    <div>
        <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
    </div>
</form>

 

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        if (Session["Cities"] != null)
        {
            ArrayList cities = new ArrayList();
            cities = (ArrayList)Session["Cities"];
            for (int i = 0; i < cities.Count; i++)
            {
                this.ListBox1.Items.Add(new ListItem(cities[i].ToString(), cities[i].ToString()));
            }
        }
    }
}

Import NameSpace:

using System.Collections;

Thank You.