Solved

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

Posted on 2009-04-07
8
704 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error running webpage local computer 7 27
Asp.Net Session Question 2 36
Problem to copy file 14 52
c#, datatable, aspx 4 23
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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:
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.

733 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