title: DataList loaded dynamically, how to store values inside between postbacks
body: <p>I am loading a Datalist to create the necessary filters (textbox's,dropdown's,checkbox's) for a grid. So far so good, the problem is between postback's.
  I understand that every time a postback occours I need to recreate the Datalist with the items, but how can I store the input made by the user on each control?</p>
  
  <p>Imagine that I have created a textbox for filtering a column and the user input "test" on the textbox and click on button that triggers the event to search the value in the gridview. </p>
  
  <p>How can I use (if so) the "viewstate" and when?</p>
  
  <p>Here is the code</p>
  
  <p>Client code:</p>
  
  <pre><code>&lt;asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"&gt;
  &lt;asp:UpdatePanel ID="UpdatePanel1" runat="server"&gt;
      &lt;Triggers&gt;
          &lt;asp:PostBackTrigger ControlID="imgHelp" /&gt;
      &lt;/Triggers&gt;
      &lt;ContentTemplate&gt;
              &lt;asp:Panel ID="pnlFilter" runat="server" Width="95%"&gt;
                  &lt;asp:DataList ID="dlFilter" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" OnItemDataBound="dlFilter_ItemDataBound" OnItemCreated="dlFilter_ItemCreated" EnableViewState="true"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:HiddenField ID="hfFilterName" runat="server" Value='&lt;%# Bind("szFilterName") %&gt;' /&gt;
                          &lt;asp:HiddenField ID="hfFilterType" runat="server" Value='&lt;%# Bind("szFilterObjType") %&gt;' /&gt;
                          &lt;asp:HiddenField ID="HiddenField1" runat="server" Value='&lt;%# Bind("szFilterFiller") %&gt;' /&gt;
                      &lt;/ItemTemplate&gt;
                      &lt;FooterTemplate&gt;
                          &lt;table&gt;
                              &lt;tr&gt;
                                  &lt;td valign="middle" style="width: 120px; text-align: right;"&gt;
                                  &lt;/td&gt;
                                  &lt;td&gt;
                                      &lt;asp:Button ID="btnSubmit" runat="server" Font-Names="Verdana" Font-Size="7pt" OnClick="btnSubmit_Click"
                                          Text="Search" /&gt;
                                  &lt;/td&gt;
                              &lt;/tr&gt;
                          &lt;/table&gt;
                      &lt;/FooterTemplate&gt;
                  &lt;/asp:DataList&gt;
              &lt;/asp:Panel&gt;
          &lt;br /&gt;
          &lt;div id="div1" style="overflow: auto; height: auto; width: 97%;"&gt;
              &lt;asp:GridView ID="gv" runat="server" CssClass="tablecloth-theme" Width="97%" GridLines="Vertical"
                  AllowSorting="True" AllowPaging="True" PageSize="20" OnPageIndexChanging="gv_PageIndexChanging"
                  OnSorting="gv_Sorting" SelectedRowStyle-BackColor="Beige" OnSelectedIndexChanged="gv_SelectedIndexChanged"&gt;
                  &lt;HeaderStyle BackColor="#7799AF" Font-Bold="True" Height="20px" Font-Names="Verdana"
                      Font-Size="7pt" ForeColor="White" HorizontalAlign="Left" /&gt;
                  &lt;Columns&gt;
                      &lt;asp:CommandField SelectText="Details" ShowSelectButton="True" /&gt;
                  &lt;/Columns&gt;
                  &lt;AlternatingRowStyle BackColor="Gainsboro" /&gt;
                  &lt;SelectedRowStyle BackColor="Beige" /&gt;
              &lt;/asp:GridView&gt;
          &lt;/div&gt;
      &lt;/ContentTemplate&gt;
  &lt;/asp:UpdatePanel&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>Server code:</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
      {
          try
          {
              ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
              if (!scriptManager.IsInAsyncPostBack)
              {
                  DataInfo_Load();
              }
  
              //Page_Prepare();
          }
          catch (Exception ex)
          {
              ShowError(ex);
          }
      }
  
  
      protected void DataInfo_Load()
      {
          try
          {
              DataSet_Load();
      Filters_Load();
              Grid_Load();
  
              if ((int)ViewState["PageStatus"] &gt;= 2)
              {
                  if (ViewState["Saveable"] != null)
                      btnNew.Visible = (bool)ViewState["Saveable"];
                  else
                      btnNew.Visible = true;
              }
              else
                  btnNew.Visible = false;
          }
          catch (Exception ex)
          {
              throw ex;
          }
      }
  
      protected void DataSet_Load()
      {
          try
          {
              Connection cn = new Connection();
              DataSet ds = null;
              string Query = hfSelStore.Value;
              SqlCommand SQLcmd = new SqlCommand(Query);
              SQLcmd.CommandType = System.Data.CommandType.StoredProcedure;
              ds = cn.ExecuteSqlCmd(cn.SqlLocalConn, SQLcmd);
              ViewState["DataSet"] = ds;
          }
          catch (Exception ex)
          {
              throw ex;
          }
      }
  
      protected void Filters_Load()
      {
          try
          {
              if (ViewState["DataSet"] == null)
              {
                  throw new Exception("No DataSet!");
              }
  
              DataSet ds = (DataSet)ViewState["DataSet"];
  
              if (ds == null)
              {
                  throw new Exception("DataSet is empty!");
              }
  
              if (ds.Tables.Count == 5)
              {
                  //contains filter
                  pnlFilter.Visible = true;
                  DataView view = new DataView(ds.Tables[4]);
                  view.Sort = "lId";
                  dlFilter.DataSource = view;
                  dlFilter.DataBind();
              }
          }
          catch (Exception ex)
          {
              throw ex;
          }
      }
  
      protected void dlFilter_ItemDataBound(object sender, DataListItemEventArgs e)
      {
          try
          {
              if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
              {
  
                  // Retrieve the Label control in the current DataListItem.
                  HiddenField hfFilterName = (HiddenField)e.Item.FindControl("hfFilterName");
                  HiddenField filtertype = (HiddenField)e.Item.FindControl("hfFilterType");
                  if (filtertype != null)
                  {
                      DataListItem dlt = ((filtertype.Parent) as DataListItem);
  
                      int itemIndex = dlt.ItemIndex;
  
                      Label lbl = new Label();
                      lbl.ID = "lbl";
                      lbl.Text = hfFilterName.Value;
                      lbl.Attributes.Add("style", "font-size:7pt; color:#005780; font-weight:bold;");
  
                      Control ctr = Control_Create(filtertype.Value);
                      ctr.ID = "val";
  
                      dlt.Controls.Add(new LiteralControl("&lt;table&gt;&lt;tr&gt;&lt;td valign=\"middle\" style=\"width: 120px; text-align: right;\"&gt;"));
                      dlt.Controls.Add(lbl);
                      dlt.Controls.Add(new LiteralControl("&lt;/td&gt;&lt;td&gt;"));
                      dlt.Controls.Add(ctr);
                      dlt.Controls.Add(new LiteralControl("&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;"));
                  }
              }
  
          }
          catch (Exception ex)
          {
              ShowError(ex);
          }
      }
  
      protected Control Control_Create(string controlType)
      {
          Control c = new TextBox();
          try
          {
              if (controlType == "TextBox")
                  c = new TextBox();
              else if (controlType == "CheckBox")
                  c = new CheckBox();
              else if (controlType == "DropDownList")
                  c = new DropDownList();
              else if (controlType == "RadioButton")
                  c = new RadioButton();
              //else if (controlType == "Checkbox")
              //    c = new TextBox();
              //else if (controlType == "Checkbox")
              //    c = new TextBox();
          }
          catch (Exception ex)
          {
              throw ex;
          }
          return c;
      }
  
      protected void btnSubmit_Click(object sender, EventArgs e)
      {
          try
          {
              gv.SelectedIndex = -1;
              gv.PageIndex = 0;
  
              Grid_Load();
          }
          catch (Exception ex)
          {
              ShowError(ex);
          }
      }
  </code></pre>
  
  <p>Can you help me out?</p>
  
  <p>Thanks in advance!</p>