title: How to create a listbox dynamically in ASP.Net
body: <pre><code>    protected override void OnInit(EventArgs e)
      {
          ListBox lst = new ListBox();
  
          lst.Attributes["class"] = "cat-list";
  
          lst.DataSource = cat.list(Convert.ToInt32(0));
          lst.DataTextField = "catName";
          lst.DataValueField = "catId";
          lst.DataBind();
  
  
          lst.AutoPostBack = true;
          lst.SelectedIndexChanged += Lst_SelectedIndexChanged;
  
          Panel pnl = new Panel();
          pnl.Attributes["class"] = "col-sm-2 col-xs-12";
          pnl.Controls.Add(lst);
  
          categories.Controls.Add(pnl);
      }
  
      private void Lst_SelectedIndexChanged(object sender, EventArgs e)
      {
          ListBox listBox = (ListBox)sender;
          ListBox lst = new ListBox();
  
          lst.Attributes["class"] = "cat-list";
  
          lst.DataSource = cat.list(Convert.ToInt32(listBox.SelectedValue));
          lst.DataTextField = "catName";
          lst.DataValueField = "catId";
          lst.DataBind();
  
  
          lst.AutoPostBack = true;
          lst.SelectedIndexChanged += Lst_SelectedIndexChanged;
  
          Panel pnl = new Panel();
          pnl.Attributes["class"] = "col-sm-2 col-xs-12";
          pnl.Controls.Add(lst);
  
          categories.Controls.Add(pnl);
      }
  </code></pre>
  
  <p>The first Listbox <code>selectedindexchanged</code> event working but second Listbox <code>selectedindexchanged</code> event not working. If I select a option on second listbox, second listbox lost.Just keep selected option on first listbox. What can I do about that? Please help me.</p>