0

I have a PlaceHolder in a webform with Master page with the code

<asp:DropDownList ID="NoofSubjectList" runat="server"     
     AutoPostBack="true" OnSelectedIndexChanged="NoofSubject_Index_Changed">
                    <asp:ListItem>1</asp:ListItem>
                    <asp:ListItem>2</asp:ListItem>
                    <asp:ListItem>3</asp:ListItem>
                    <asp:ListItem>4</asp:ListItem>
                    <asp:ListItem>5</asp:ListItem>
                    <asp:ListItem>6</asp:ListItem>
                    <asp:ListItem>7</asp:ListItem>
                    <asp:ListItem>8</asp:ListItem>
                    <asp:ListItem>9</asp:ListItem>
                    <asp:ListItem>10</asp:ListItem>
                    <asp:ListItem>11</asp:ListItem>
                    <asp:ListItem>12</asp:ListItem>
</asp:DropDownList>
<asp:PlaceHolder ID="placeholder" runat="server" />    

Based on the number(n) selected from the DropDownList (1), I will be Dynamically creating two sets of DropDownList (2,3) for the selected n numbers. In that the first DropDownList loads data from a database. The code is shown below,

protected void NoofSubject_Index_Changed(Object sender, EventArgs e)
    {
      int value = Convert.ToInt32(NoofSubjectList.SelectedItem.Text.ToString());
      DataSet ds1 = new DataSet();
      MySql.Data.MySqlClient.MySqlConnection mysqlConnection = new     
      MySql.Data.MySqlClient.MySqlConnection    
      ("Database=school;Server=localhost;UID=root;PWD=;");
      MySql.Data.MySqlClient.MySqlCommand mysqlCommand = new     
      MySql.Data.MySqlClient.MySqlCommand    
      ("SELECT Dept FROM tabledept WHERE Status='Active'", mysqlConnection);
      MySql.Data.MySqlClient.MySqlDataAdapter mysqlAdaptor = new      
      MySql.Data.MySqlClient.MySqlDataAdapter(mysqlCommand);
      mysqlAdaptor.Fill(ds1);
      for (int i = 0; i <= value - 1; i++)
      {
         DropDownList _SubList = new DropDownList();
         _SubList.ID = "SubList" + (i + 1);
         _SubList.Width= 75;
         DropDownList _DeptList = new DropDownList();
         _DeptList.ID = "DeptList" + (i + 1);              
         _DeptList.Width = 75;
         _DeptList.SelectedIndexChanged += DeptList_Index_Changed;
         Literal _spacer = new Literal();
         _spacer.Text = "<br />";
         Literal _spacerlbl = new Literal();
         _spacerlbl.Text = "&nbsp;&nbsp;";
         Label _Lbl = new Label();
         _Lbl.ID = "lbl_Subject" + i;
         _Lbl.Text = "Subject" + (i+1);
         Label _Lbl1 = new Label();
         _Lbl1.ID = "lbl_Dept" + i;
         _Lbl1.Text = "Department";
         _DeptList.DataSource = ds1;
         _DeptList.DataTextField = ds1.Tables[0].Columns["Dept"].ColumnName;
         _DeptList.DataValueField = ds1.Tables[0].Columns["Dept"].ColumnName;
         _DeptList.DataBind();
         _DeptList.AutoPostBack = true;
         placeholder.Controls.Add(_Lbl1);
         placeholder.Controls.Add(_spacerlbl);
         placeholder.Controls.Add(_DeptList);
         placeholder.Controls.Add(_spacerlbl);
         placeholder.Controls.Add(_Lbl);
         placeholder.Controls.Add(_SubList);
         placeholder.Controls.Add(_spacer);
      }
  }
protected void DeptList_Index_Changed(Object sender, EventArgs e)
  {
     \\Based on the selection in DropDownList2, a list will be loaded from a     
     \\database to DropDownList3      
  }    

The above event is triggered based on the selection of the number n from the DropDownList (1). If I select an item from DropDownList (2) the data will be generated from database and added to the DropDownList (3) . The problem is when I try to select an item from DropDownList (2), the dynamically created controls get lost. How to overcome this issue?

I even googled it and I found out that I am missing something called PostBack in my code. But I could not able to find out relevant resource to learn about it

Dhinesh
  • 181
  • 2
  • 5
  • 20

1 Answers1

0

I got a link which shows an easy example of creating the controls after postback.

Creating Dynamic TextBox Controls using C#

Dhinesh
  • 181
  • 2
  • 5
  • 20