如何将事件处理程序添加到同一事件处理程序中的控件



我在 AJAX 更新面板中有 2 个下拉列表,一行中有 7 个文本框;每次在第二个下拉列表中选择项目时,我都需要创建一个新的控件行;并将相同的事件处理程序添加到新创建的第二个下拉列表中。等。

问题不在第一行 ddl 中,因为它在 HTML 中; 但是当我在创建的新行 ddl 中选择一个新项目时,onSelectedIndexChanged 不会触发,第二行消失了!!

有什么建议吗??

我的网页代码:

 <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>
                            &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                        <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>
                            &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                        <asp:TextBox runat="server" ID="textbox1" CssClass="txtbx"></asp:TextBox>
                            &nbsp&nbsp&nbsp&nbsp
                        <asp:TextBox runat="server" ID="textbox2" CssClass="txtbx"></asp:TextBox>
                            &nbsp&nbsp&nbsp&nbsp
                        <asp:TextBox runat="server" ID="textbox3" CssClass="txtbx"></asp:TextBox>
                            &nbsp&nbsp&nbsp&nbsp
                        <asp:TextBox runat="server" ID="textbox4" CssClass="txtbx"></asp:TextBox>
                            &nbsp&nbsp&nbsp&nbsp
                        <asp:TextBox runat="server" ID="textbox5" CssClass="txtbx"></asp:TextBox>
                            &nbsp&nbsp&nbsp&nbsp
                        <asp:TextBox runat="server" ID="textbox6" CssClass="txtbx"></asp:TextBox>
                            &nbsp&nbsp&nbsp
                        <asp:TextBox runat="server" ID="textbox7" CssClass="txtbx"></asp:TextBox>
                            <br />
                            <br />
                        </ContentTemplate>
                    </asp:UpdatePanel>

代码隐藏:

 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("&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp"));
        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("&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp"));
        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("&nbsp&nbsp&nbsp&nbsp&nbsp"));
            Session["txtcounter"] = (int)Session["txtcounter"] + 1;
        }
        UpdatePanel1.ContentTemplateContainer.Controls.Add(new LiteralControl("<br /><br />"));
        Session["ddlcounter"] = (int)Session["ddlcounter"] + 1;
    }
}
我相信

问题是,当第二个动态生成的 ddl 回发时,无法记住上一步中动态呈现的控件是什么。

我建议你阅读这篇文章;我认为您错过了最后一步,即上述链接中的重新创建控件功能。

希望有帮助。

相关内容

  • 没有找到相关文章

最新更新