Solved

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

Posted on 2009-04-07
8
676 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.

746 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