ASP.net with C# Keeping a List on postback

14,450

Solution 1

Just use this property to store information:

public List<string> Code
{
    get
    {
        if(HttpContext.Current.Session["Code"] == null)
        {
            HttpContext.Current.Session["Code"] = new List<string>();
        }
        return HttpContext.Current.Session["Code"] as List<string>;
    }
    set
    {
        HttpContext.Current.Session["Code"] = value;
    }

}

Solution 2

This is an oddity in ASP.NET. Whenever you programmatically add items to a collection control (listbox, combobox), you must re-populate the control on each postback.

This is because the Viewstate only knows about items added during the page rendering cycle. Adding items at the client-side only works the first time, then the item is gone.

Try this:

public partial class Main : System.Web.UI.Page
{

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                 Session["MyList"] = new List<string>();
            }   
            ComboBox cbo = myComboBox; //this is the combobox in your page
            cbo.DataSource = (List<string>)Session["MyList"];
            cbo.DataBind();
        }




        protected void cmdAdd_Click(object sender, EventArgs e)
        {
            List<string> code = Session["MyList"];
            code.Add(lstCode.Text);
            Session["MyList"] = code;  
            myComboBox.DataSource = code;
            myComboBox.DataBind();
        }
    }

Solution 3

You can't keep values between post backs.

You can use session to preserve list:

// store the list in the session
List<string> code=new List<string>();

protected void Page_Load(object sender, EventArgs e)
{
 if(!IsPostBack)
  Session["codeList"]=code;

}
 // use the list
void fn()
{
 code=List<string>(Session["codeList"]); // downcast to List<string> 
 code.Add("some string"); // insert in the list
 Session["codeList"]=code; // save it again
}
Share:
14,450
Jake Gaston
Author by

Jake Gaston

Updated on June 24, 2022

Comments

  • Jake Gaston
    Jake Gaston almost 2 years

    My situation goes like this: I have these lists with data inserted into them when a user presses an ADD button, but I guess on postback the Lists are re-zeroed. How do you keep them preserved? I've been looking for the answer, but I guess I don't quite understand how to use the session, etc.

    I'm very new to ASP.net and not much better with C# it would seem.

    public partial class Main : System.Web.UI.Page
    {
    
    
     List<string> code = new List<string>();
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
        //bleh   
    
    }
    
    protected void cmdAdd_Click(object sender, EventArgs e)
    {
    
        code.Add(lstCode.Text);
    }
    
  • Simon Dugré
    Simon Dugré about 12 years
    This is, according to me, the best way to do this.
  • Jake Gaston
    Jake Gaston about 12 years
    Hey, thanks, I'll try this out. What is the namespace (using System...) to get the session keyword to work, though?
  • Vano Maisuradze
    Vano Maisuradze about 12 years
    @JakeGaston just copy this property into your Main class. And instead of your "code" variable, use "Code" property.
  • Rezo Megrelidze
    Rezo Megrelidze almost 10 years
    Session is not a keyword. It's a property of type HttpSessionState. You can access it like this HttpContext.Current.Session.
  • Shahram Banazadeh
    Shahram Banazadeh over 5 years
    is using Session correct way for this? may be user has two open tabs of the same page and wants works on two different copies of the same page...
  • RyanN1220
    RyanN1220 over 5 years
    @VanoMaisuradze I'm having a similar issue, can you tell me what you mean by use the Code "property?"
  • Vano Maisuradze
    Vano Maisuradze over 5 years
    "Code" is just a name of the property. It could be any other name. If you want to know what property is, take a look: docs.microsoft.com/en-us/dotnet/csharp/properties