如何添加一个事件处理程序来控制同一个事件处理程序?

时间:2016-07-10 09:08:41

标签: c# asp.net ajax webforms event-handling

我在AJAX更新面板中的一行中有2个带有7个文本框的下拉列表;我需要在每次选择第二个下拉列表中的项目时创建一个新的控件行;并将相同的eventhandler添加到新创建的第二个dropdownlist..etc。

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

有什么建议吗?

我的HTML代码:

 <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;
    }
}

1 个答案:

答案 0 :(得分:0)

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

我建议你通过this article;我想你错过了最后一步,即上述链接中的 RecreateControls 功能。

希望有所帮助。