Solved

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

Posted on 2009-04-07
8
722 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

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.
Learn about the eCommerce marketing trends for the year ahead.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
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…

623 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