title: How to Store all attribute values of ListBox on postback?(asp.net)
body: <p>I have a problem here!
  Hi Dear Experts.</p>
  
  <p>I have a custom control of asp:ListBox as Code Below :</p>
  
  <pre><code>using System;
  using System.ComponentModel;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  namespace ExcelLikeFilterGridView
  {
      [DefaultProperty("Text")]
      [ToolboxData("&lt;{0}:AADList runat=server&gt;&lt;/{0}:AADList&gt;")]
      public class AADListBox : ListBox
      {
          [Bindable(true)]
          [Category("Appearance")]
          [DefaultValue("")]
          [Localizable(true)]
  
          protected override object SaveViewState()
          {
              // create object array for Item count + 1
              object[] allStates = new object[this.Items.Count + 1];
  
              // the +1 is to hold the base info
              object baseState = base.SaveViewState();
              allStates[0] = baseState;
  
              Int32 i = 1;
              // now loop through and save each Style attribute for the List
              foreach (ListItem li in this.Items)
              {
                  Int32 j = 0;
                  string[][] attributes = new string[li.Attributes.Count][];
                  foreach (string attribute in li.Attributes.Keys)
                  {
                      attributes[j++] = new string[] { attribute, li.Attributes[attribute] };
                  }
                  allStates[i++] = attributes;
              }
              return allStates;
          }
          protected override void LoadViewState(object savedState)
          {
              if (savedState != null)
              {
                  object[] myState = (object[])savedState;
  
                  // restore base first
                  if (myState[0] != null)
                      base.LoadViewState(myState[0]);
  
                  Int32 i = 1;
                  foreach (ListItem li in this.Items)
                  {
                      // loop through and restore each style attribute
                      foreach (string[] attribute in (string[][])myState[i++])
                      {
                          li.Attributes[attribute[0]] = attribute[1];
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>In my Override methods i want to get all attributes from each ListBoxItem, on postbacks.
  here is my code behind :</p>
  
  <pre><code>foreach (DataColumn col in dv.Table.Columns)
  {
                  ListItemCollection Fields = new ListItemCollection();
                  ListItem lst = new ListItem(col.ColumnName.ToString(), col.DataType.Name);
                  if (col.DataType.Name == "String")
                  {
                      lst.Attributes.Add("db-control-like", "");
                      lst.Attributes.Add("db-control-equal", "");
                  }
                  else if (col.DataType.Name == "Decimal" || col.DataType.Name == "Int32")
                  {
                      lst.Attributes.Add("db-control-from", "");
                      lst.Attributes.Add("db-control-to", "");
                  }
                  Fields.Add(lst);
                  lstboxFields.Items.AddRange(Fields.Cast&lt;ListItem&gt;().ToArray());
  }
  </code></pre>
  
  <p>here is my HTML OUTPUT :</p>
  
  <pre><code>    &lt;select size="4" name="lstboxFields" id="lstboxFields" class="lstbox fields"&gt;
      &lt;option value="Decimal" db-control-from="12" db-control-to="123123"&gt;fg_SysCode&lt;/option&gt;
      &lt;option value="Decimal" db-control-from="asda" db-control-to="12312"&gt;fg_GrMaster&lt;/option&gt;
      &lt;option value="String" db-control-equal="GridLink2,GridLink1" db-control-like="2321312"&gt;fg_GrName&lt;/option&gt;
      &lt;option value="Int32" db-control-from="1" db-control-to="2"&gt;fg_Len&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>i set the attribute values with javascript and then on postback just first ListItem attribute's are here yet(Attention : WithOut Values).</p>
  
  <p>Like this :</p>
  
  <pre><code>&lt;select size="4" name="lstboxFields" id="lstboxFields" class="lstbox fields"&gt;
  &lt;option selected="selected" value="Decimal" db-control-from="" db-control-to=""&gt;fg_SysCode&lt;/option&gt;
  &lt;option value="Decimal" db-control-from="" db-control-to=""&gt;fg_GrMaster&lt;/option&gt;
  &lt;option value="String" db-control-like="" db-control-equal=""&gt;fg_GrName&lt;/option&gt;
  &lt;option value="Int32" db-control-from="" db-control-to=""&gt;fg_Len&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>SO</p>
  
  <p>Where is the problem in my overrides?
  I Want to Have All Attributes Values Back in postbacks..!
  Please help me on this
  Thanks all</p>