I have 2 Dropdownlists with 7 textboxes in one line in AJAX update Panel; i need to create a new line of controls on each time i select an item in the second dropdownlist; and add the same eventhandler to the new created second dropdownlist..etc.
the problem is not in the first line ddl because its in HTML; but when i select a new item in the created new line ddl, the onSelectedIndexChanged does not fire, and the second line disappears!!
Any suggestions??
My HTML Code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:DropDownList runat="server" ID="DropDownList1" CssClass="dropdownlist" EnableViewState="true"> <asp:ListItem>a</asp:ListItem> <asp:ListItem>b</asp:ListItem> </asp:DropDownList>                                          <asp:DropDownList runat="server" ID="DropDownList2" CssClass="dropdownlist" AutoPostBack="true" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" EnableViewState="true"> <asp:ListItem>a</asp:ListItem> <asp:ListItem>b</asp:ListItem> </asp:DropDownList>                                   <asp:TextBox runat="server" ID="textbox1" CssClass="txtbx"></asp:TextBox>      <asp:TextBox runat="server" ID="textbox2" CssClass="txtbx"></asp:TextBox>      <asp:TextBox runat="server" ID="textbox3" CssClass="txtbx"></asp:TextBox>      <asp:TextBox runat="server" ID="textbox4" CssClass="txtbx"></asp:TextBox>      <asp:TextBox runat="server" ID="textbox5" CssClass="txtbx"></asp:TextBox>      <asp:TextBox runat="server" ID="textbox6" CssClass="txtbx"></asp:TextBox>     <asp:TextBox runat="server" ID="textbox7" CssClass="txtbx"></asp:TextBox> <br /> <br /> </ContentTemplate> </asp:UpdatePanel>
Code behind:
public partial class WebForm1 : System.Web.UI.Page
{
DropDownList ddl1;
DropDownList ddl2;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["ddlcounter"] = 1;
}
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
ddl1 = new DropDownList();
ddl1.ID = "ddl1_" + (int)Session["ddlcounter"];
ddl1.Width = 200;
ddl1.Height = 30;
ddl1.Items.Add("a");
ddl1.Items.Add("b");
UpdatePanel1.ContentTemplateContainer.Controls.Add(ddl1);
UpdatePanel1.ContentTemplateContainer.Controls.Add(new LiteralControl("                                          "));
ddl2 = new DropDownList();
ddl2.ID = "ddl2_" + (int)Session["ddlcounter"];
ddl2.Width = 200;
ddl2.Height = 30;
ddl2.Items.Add("a");
ddl2.Items.Add("b");
ddl2.AutoPostBack = true;
UpdatePanel1.ContentTemplateContainer.Controls.Add(ddl2);
UpdatePanel1.ContentTemplateContainer.Controls.Add(new LiteralControl("                                      "));
ddl2.SelectedIndexChanged += new EventHandler(DropDownList2_SelectedIndexChanged);
Session["txtcounter"] = 1;
for (int i = 0; i < 7; i++)
{
TextBox txt = new TextBox();
txt.Width = 45;
txt.Height = 25;
UpdatePanel1.ContentTemplateContainer.Controls.Add(txt);
UpdatePanel1.ContentTemplateContainer.Controls.Add(new LiteralControl("     "));
Session["txtcounter"] = (int)Session["txtcounter"] + 1;
}
UpdatePanel1.ContentTemplateContainer.Controls.Add(new LiteralControl("<br /><br />"));
Session["ddlcounter"] = (int)Session["ddlcounter"] + 1;
}
}
I believe The problem is that when the second dynamically generated ddl postsback, there is no way to remember what were the dynamically rendered controls in the previous step.
I suggest you go through this article ; I think you miss the final step, ie the RecreateControls function in the aforementioned link.
Hope that helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.