title: Viewstate null on postback
body: <p>So I have a listbox on my page and some textfields. Through the textfields I can add an item to my listbox (click the button, it adds it to a private List which is then set as a ViewState and the list is databound again). My listbox is also in an updatepanel which gets triggered on the button's Click event. Problem: My Viewstate remains null on a postback so it gets reset each time.</p>
  
  <p>Some code:</p>
  
  <pre><code>private const string VIEW_INGREDIENTS = "IngredientsList";
          private const string VIEW_LANGUAGE = "CurrentLanguage";
          private List&lt;IngredientData&gt; _ingredientsList;
  
  protected void Page_PreInit(object sender, EventArgs e)
          {
              if (Page.IsPostBack)
              {
                  if (ViewState[VIEW_INGREDIENTS] != null)
                  {
                      _ingredientsList = (List&lt;IngredientData&gt;) ViewState[VIEW_INGREDIENTS];
                  }
  
              }
              else
              {
                  // prepare ingredient lists
                  _ingredientsList = new List&lt;IngredientData&gt;();
              }
          }
  
          protected void Page_Load(object sender, EventArgs e)
          {
  
              lstIngredients.DataSource = _ingredientsList;
              lstIngredients.DataTextField = "Text";
              lstIngredients.DataValueField = "Name";
              lstIngredients.DataBind();
          }
  
  protected void btnAddIngredient_Click(object sender, EventArgs e)
          {
              _ingredientsList.Add(new IngredientData { Name = txtIngredientName.Text, Quantity = txtUnitQuantity.Text, Unit = lstUnits.SelectedValue });
  
              ViewState[VIEW_INGREDIENTS] = _ingredientsList;
              lstIngredients.DataSource = _ingredientsList;
              lstIngredients.DataBind();
          }
  </code></pre>