How to Store StringColelction in Session Varible
In this article we are going to see haw can we use Session State Management technique to store the StringCollection and used it in Different page within the Website. This will also explain the concept of Wizard how we can use wizard in our programs.
Session is used for maintaning state across the pages.
Storing Collection types in in Session variable
For this i have used wizard control which is used to show different controls on different link.
Steps:
1. Take a wizard control -> click on right top arrow -> select add/remove wizard steps.
2. Add 3 items in it a. Select your favourite book(set the id property to Books)
b. Favourite list
c. Finish
3. Click on first link ie. Select your favourite book and take a checkboxlist on the right side of the wizard. Add some books to it.
similarly click on second and third link and listbox and label(text= Thank for visiting).
4. Generate event of next button by double click on it.
5. Add System.Collections.Specialized namespace.
6. Add below code on next button event:
protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
{
ListBox1.Items.Clear();
StringCollection product;
if (Wizard1.ActiveStep.ID == "Books")
{
product = new StringCollection();
foreach (ListItem i in CheckBoxList1 .Items )
{
if (i.Selected )
{
product.Add(i.ToString());
}
Session["cart"] = product;
}
if (Session ["cart"]!=null )
{
// using session for storing collection datatype
product =(StringCollection ) Session["cart"];
foreach (string i in product )
{
ListBox1.Items.Add(i);
}
}
}
}
On finishButton event add below code:
protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{
Response.Redirect("ThankU.aspx");
}
7. Take a listbox on Thanku page.
Add below code on page load event:
protected void Page_Load(object sender, EventArgs e)
{
ListBox1.Items.Clear ();
if (Session ["cart"] != null )
{
StringCollection product = (StringCollection )Session["cart"];
foreach (string i in product )
{
ListBox1.Items.Add(i);
}
}
}