ASP.. NET在c#中动态创建控件:失败事件

本文关键字:控件 失败 事件 创建 动态 NET ASP | 更新日期: 2023-09-27 18:07:01

我正在创建一个web应用程序,需要在单击按钮时动态创建控件,然后将控件添加到已经构建的asp:表中。

问题是动态控件的事件不触发(ciDD_SelectedIndexChanged)。

按顺序:单击按钮,它为控件id设置一个Session变量,创建控件,然后将它们添加到表行。这是OnClick完成的,OnInit用于post back。

是否有一种简单的方法来设置动态控件的事件,或者我正在以一种可怕的方式去做这件事?

按钮及其事件:

    <asp:ImageButton ID="addHardware" runat="server" ImageUrl="~/Images/plus.png" Height="24" Width="24" OnClick="addHardware_Click" ImageAlign="AbsMiddle" style="margin-left:7px" Visible="false" />
    protected void addHardware_Click(object sender, ImageClickEventArgs e)
    {
        Session["rowCount"] = (Convert.ToInt32(Session["rowCount"]) + 1); 
        CreateControls(Convert.ToInt32(Session["rowCount"]));
        CreateRow(Convert.ToInt32(Session["rowCount"]));
    }

控件创建函数和保存到的列表:

    private static List<Control> _controls = new List<Control>();
    private static List<Control> HWControls
    {
        get { return _controls; }
    }
    protected void CreateControls(int i)
    {
        DropDownList ci = new DropDownList();
        ci.AutoPostBack = true;
        ci.ID = "ciDD" + i;
        ci.SelectedIndexChanged += new EventHandler(this.ciDD_SelectedIndexChanged);
        ci.Items.Add(new ListItem("", ""));
        ci.Items.Add(new ListItem("test1", "test1"));
        ci.Items.Add(new ListItem("test2", "test2"));
        DropDownList dt = new DropDownList();
        dt.ID = "deviceTypeDD" + i;
        DropDownList m = new DropDownList();
        m.ID = "modelDD" + i;
        TextBox qt = new TextBox();
        qt.ID = "qtTB" + i;
        DropDownList dv = new DropDownList();
        dv.ID = "deviceNumDD" + i;
        TextBox sn = new TextBox();
        sn.ID = "serialTB" + i;
        HWControls.Add(ci);
        HWControls.Add(dt);
        HWControls.Add(m);
        HWControls.Add(qt);
        HWControls.Add(dv);
        HWControls.Add(sn);
    }

行创建函数,以及它对应的List:

    private static List<TableRow> _rows = new List<TableRow>();
    private static List<TableRow> Rows
    {
        get { return _rows; }
    }
    protected void CreateRow(int i)
    {
        TableRow row = new TableRow();
        TableCell cell = new TableCell();
        TableCell cell1 = new TableCell();
        TableCell cell2 = new TableCell();
        TableCell cell3 = new TableCell();
        TableCell cell4 = new TableCell();
        TableCell cell5 = new TableCell();
        cell.Controls.Add(HWControls.Find(x => x.ID.Contains("ciDD" + i)));
        cell1.Controls.Add(HWControls.Find(x => x.ID.Contains("deviceTypeDD" + i)));
        cell2.Controls.Add(HWControls.Find(x => x.ID.Contains("modelDD" + i)));
        cell3.Controls.Add(HWControls.Find(x => x.ID.Contains("qtTB" + i)));
        cell4.Controls.Add(HWControls.Find(x => x.ID.Contains("deviceNumDD" + i)));
        cell5.Controls.Add(HWControls.Find(x => x.ID.Contains("serialTB" + i)));
        row.Cells.Add(cell);
        row.Cells.Add(cell1);
        row.Cells.Add(cell2);
        row.Cells.Add(cell3);
        row.Cells.Add(cell4);
        row.Cells.Add(cell5);
        Rows.Add(row);
        hardwareTable.Rows.Add(row);
    }

返回后重新创建控件/行的OnInit:

    protected override void OnInit(EventArgs e)
    {
        int i = Convert.ToInt32(Session["rowCount"]);
        if (i != 0)
        {
            for (int j = 1; j <= i; j++)
            {
                CreateControls(j);
                CreateRow(j);
            }
        }
        base.OnInit(e);
    }

ASP.. NET在c#中动态创建控件:失败事件

我发现在PreInit阶段创建事件处理程序解决了这个问题。

    protected void Page_PreInit(object sender, EventArgs e)
    {
        foreach (Control c in HWControls)
        {
            if(c.ID.Contains("ciDD"))
            {
                ((DropDownList)c).SelectedIndexChanged += new EventHandler(this.ciDD_SelectedIndexChanged);
            }
            else if (c.ID.Contains("deviceTypeDD"))
            {
                ((DropDownList)c).SelectedIndexChanged += new EventHandler(this.deviceTypeDD_SelectedIndexChanged);
            }
        }
    }