Solved

How do u store listbox item in session and retrieve them in another aspx page?

Posted on 2009-04-07
8
692 Views
Last Modified: 2013-11-18
I am using C# in .net framework i have a Gridview table with items from a database. selected items from this gridview are then moved to a listbox. i need to store these items in the listbox and retrieve them from session in another aspx page.
coding-used.txt
0
Comment
Question by:MUDDY_123
  • 5
  • 3
8 Comments
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24086063
Should be able to store whole listbox in session:

 ListBox1.Items.Add("hello");
            ListBox1.Items.Add("hello1");
            ListBox1.Items.Add("hello2");
            ListBox1.Items.Add("hello3");

            Session["myListBox"] = ListBox1;

            ListBox listnew = Session["myListBox"] as ListBox;

            foreach (var item in listnew.Items)
            {
                Response.Write(item.ToString());
            }
0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24086083
concerned of memory ? can do this:
   StringBuilder sb = new StringBuilder();        
        foreach (ListItem i in lst)
        {
            sb.Append(i.Text); sb.Append("|");
        }
        Session["Items"] = sb.ToString();
        foreach (string s in Session["Items"].ToString().Split(new char['|'], StringSplitOptions.RemoveEmptyEntries))
        {
            lst.Items.FindByText(s).Selected = true;
        }
0
 

Author Comment

by:MUDDY_123
ID: 24086201
thanks for quick response this is the coding i have in my cart.apsx.cs file

   if (e.CommandName == "ADD")
                    {
                                               
                    int index = Convert.ToInt32(e.CommandArgument);
                    GridViewRow selectedRow = GridView1.Rows[index];
                    TableCell ProductID = selectedRow.Cells[0];

                    TableCell PName = selectedRow.Cells[1];
                    TableCell PDescription = selectedRow.Cells[2];
                    TableCell Price = selectedRow.Cells[3];
                    int Quantity = 1;

                    ListItem item = new ListItem("Product Name: "+PName.Text + "  " +"Product Description: " + PDescription.Text + "  " +"Price Per Item: £"+ Price.Text + "  "+"Quantity: "+ Quantity);
                    ListItem item1 = new ListItem(Price.Text);
                         

                   
                        ListBox1.Items.Add(item);
                        Session["Cart1"] = ListBox1;
                    }
                 
                 
               
}

Hopefully the Session[cart1] = listbox1 should store the listbox into session,
what i cant work out is how to reteive this listbox from another aspx page called checkout from the page_load method.


0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24086270
From Page2 (where want to access the listbox from session)
public partial class Page2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ListBox listnew = Session["myListBox"] as ListBox;

            foreach (var item in listnew.Items)
            {
                Response.Write(item.ToString());
            }
        }
    }
0
 

Author Comment

by:MUDDY_123
ID: 24086585
thanks for response

instead of response.write

i need to be able to add each item to a listbox in page 2

can you help me with this
0
 
LVL 9

Accepted Solution

by:
Sreedhar Vengala earned 500 total points
ID: 24086623
as required:

protected void Page_Load(object sender, EventArgs e)
        {
            ListBox listnew = Session["myListBox"] as ListBox;
            ListBox newListBox = new ListBox();

            foreach (var item in listnew.Items)
            {
               
                newListBox.Items.Add(item.ToString());
            }

            form1.Controls.Add(newListBox);
        }
0
 

Author Comment

by:MUDDY_123
ID: 24086715
thanks for help
0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24086729
Always there to help out.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Every business owner understands the significance of online customer reviews and the impact it can have on sales and revenues. With technology advancing at such a rapid pace, getting online reviews has never been easier, especially when many regions…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question