回发不适用于动态添加项目下拉列表

时间:2015-04-09 02:28:56

标签: c# asp.net

我正在动态添加下拉列表我很成功但是当下拉列表选择的索引发生更改时不会触发事件。当页面中预先存在的添加按钮时会触发它。这是我的代码:

设计页面:

<asp:Panel ID="textboxespanel" runat="server">
    <asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" />
</asp:Panel>

Сode隐藏:

protected override void LoadViewState(object savedState)
{
    base.LoadViewState(savedState);
    controlidlist = (List<string>)ViewState["controlidlist"];
    foreach (string Id in controlidlist)
    {
        i++;
        TextBox tb = new TextBox();
        tb.ID = Id;
        LiteralControl lineBreak = new LiteralControl();
        DropDownList dl = new DropDownList();
        dl.ID = "dropdownlist" + i;
        dl.DataTextField = "cloth";
        dl.DataValueField = "cloth";
        dl.DataSource = obj.getclothitems();

        dl.DataBind();
        DropDownList dldl = new DropDownList();
        dldl.ID = "dropdownlistdropdownlist" + i;
        dldl.Items.Insert(0, "MALE");
        dldl.Items.Insert(0, "FEMALE");
        dldl.Items.Insert(0, "HOME");

        dldl.SelectedIndexChanged += dropdownlistchanged1;
        dldl.AutoPostBack = true;
        //PlaceHolder1.Controls.Add(tb);
        //PlaceHolder1.Controls.Add(lineBreak);

        textboxespanel.Controls.Add(tb);
        textboxespanel.Controls.Add(lineBreak);
        textboxespanel.Controls.Add(dldl);
        textboxespanel.Controls.Add(lineBreak);
        textboxespanel.Controls.Add(dl);
        textboxespanel.Controls.Add(lineBreak);
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    i++;
    TextBox tb = new TextBox();
    tb.ID = "textboxes" + i;
    tb.Text = "textbox" + i;
    DropDownList dldl = new DropDownList();
    dldl.ID = "dropdownlistdropdownlist" + i;
    dldl.Items.Insert(0, "MALE");
    dldl.Items.Insert(0, "FEMALE");
    dldl.Items.Insert(0, "HOME");

    dldl.SelectedIndexChanged += dropdownlistchanged1;
    dldl.AutoPostBack = true;

    DropDownList dl = new DropDownList();
    dl.ID = "dropdownlist" + i;
    dl.DataTextField = "cloth";
    dl.DataValueField = "cloth";
    dl.DataSource = obj.getclothitems();

    dl.DataBind();

    LiteralControl lineBreak = new LiteralControl("<br>");
    //PlaceHolder1.Controls.Add(tb);
    //PlaceHolder1.Controls.Add(lineBreak);
    textboxespanel.Controls.Add(tb);
    textboxespanel.Controls.Add(lineBreak);
    textboxespanel.Controls.Add(dldl);
    textboxespanel.Controls.Add(lineBreak);
    textboxespanel.Controls.Add(dl);
    textboxespanel.Controls.Add(lineBreak);
    controlidlist.Add(tb.ID);
    ViewState["controlidlist"] = controlidlist;
}

public void dropdownlistchanged1(object sender, EventArgs e)
{
    DropDownList lbtn = (DropDownList)sender;
    string id = lbtn.ID;
}

2 个答案:

答案 0 :(得分:1)

您需要在Init Event中添加控件,有关ASP.NET Page Life Cycle Overview

的更多信息
protected override void OnInit(EventArgs e){
base.OnInit(e);
controlidlist = ViewState["controlidlist"] as List<string>;
if(controlidlist !=null)
{
foreach (string Id in controlidlist)
{
    i++;
    TextBox tb = new TextBox();
    tb.ID = Id;
    LiteralControl lineBreak = new LiteralControl();
    DropDownList dl = new DropDownList();
    dl.ID = "dropdownlist" + i;
    dl.DataTextField = "cloth";
    dl.DataValueField = "cloth";
    dl.DataSource = obj.getclothitems();

    dl.DataBind();
    DropDownList dldl = new DropDownList();
    dldl.ID = "dropdownlistdropdownlist" + i;
    dldl.Items.Insert(0, "MALE");
    dldl.Items.Insert(0, "FEMALE");
    dldl.Items.Insert(0, "HOME");

    dldl.SelectedIndexChanged += dropdownlistchanged1;
    dldl.AutoPostBack = true;
    //PlaceHolder1.Controls.Add(tb);
    //PlaceHolder1.Controls.Add(lineBreak);

    textboxespanel.Controls.Add(tb);
    textboxespanel.Controls.Add(lineBreak);
    textboxespanel.Controls.Add(dldl);
    textboxespanel.Controls.Add(lineBreak);
    textboxespanel.Controls.Add(dl);
    textboxespanel.Controls.Add(lineBreak);
}
}
}

答案 1 :(得分:1)

在将整个面板放入更新面板后,它可以正常工作。

     <asp:UpdatePanel runat="server">
        <ContentTemplate>


    <asp:Panel ID="textboxespanel" runat="server">
        <asp:Button ID="Button1" runat="server" Text="Add" OnClick="Button1_Click" />
        <asp:Button ID="bbb" runat="server" Text="cc" OnClick="bbb_Click" />

    </asp:Panel>
 </ContentTemplate>
    </asp:UpdatePanel>