Mickey
Mickey

Reputation: 149

Session help, with store and display on gridview

How do I create one session with a list? Can one session contain many attributes? After create how to create on a gridview

if (Session["Cart"] == null)
{
   Session["Cart"] = new List<string>() { Id };
   var name = (List<string>)Session["Cart"];
   name.Add(lblName.Text);
   var Qty = (List<string>)Session["Cart"];
   Qty.Add(txtAddtoCart.Text);
   var Price = (List<string>)Session["Cart"];
   Price.Add(lblPrice.Text);

   if (lblProductStates.Visible == true)
   {
      var Promotion = (List<string>)Session["Cart"];
      Promotion.Add(lblProductStates.Text);
   }
}

<asp:SqlDataSource ID="BasketData" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" >
  </asp:SqlDataSource>

Upvotes: 1

Views: 924

Answers (1)

Pranav
Pranav

Reputation: 8871

You can directly store List in session :-

for example :-

List<string> lt=new List<string>();
lt.Add("first");

Then store it in session:-

Session["Cart"]=lt;

When you want to retrieve data from session(on same or another page), you can do like this :-

List<string> lst=(List<string>)Session["cart"];

and Now finally bind it to gridview:-

GridView1.DataSource = lst;// Note gridview1 is your gridview ID
GridView1.DataBind(); 

Hope this helps.

Upvotes: 1

Related Questions