Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to persist Dictionary object on pageload .
My code is

C#
 public partial class abc : System.Web.UI.Page
{
        
        Dictionary<string, string> CategoryList = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            cm = new CommonMethods();
         
            CategoryList = cm.GetCategory();
    
            if (!IsPostBack)
            {

                DDLCategory.DataSource = CategoryList.Values;
                DDLCategory.DataBind();
            }
        }
}

It is working fine but when I write code like this
C#
if (!IsPostBack)
{
    CategoryList = cm.GetCategory();
    DDLCategory.DataSource = CategoryList.Values;
    DDLCategory.DataBind();
}

CategoryList is null(after first time pageload) and for this I using session object.
I want to know that make session object is correct or not if not then what should I do?
Posted
Comments
piyush_singh 5-Dec-13 8:27am    
If the value of the dropdownlist is not affected by postback, then you should definitely set its datasource only once inside the !ISPostBack. Now since, on each postback, the whole page is rendered, you need to store your custom values (such as Dictionary in this case) somewhere. That's where comes, Sessions & ViewStates. If you want to persist your data across multiple pages then, use Session. If you want to hold the value as long as the current page is active then, use ViewState. So that the values will be cleared when the user navigates to some other pages. Since session variables remains active as long as the current session is active it is usually advised not to keep heavy objects in it as it will slow down your application.
Member 7909353 5-Dec-13 23:36pm    
I am using session, but want to know another way if any.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900