Solved

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

Posted on 2009-04-07
8
685 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Are you using email marketing software? If not, you're missing out on effortless marketing and the reaching of desired conversion rates through email marketing software.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now