【问题标题】:ASP.NET Dynamically created controls in C#: failed eventsASP.NET 在 C# 中动态创建的控件:失败的事件
【发布时间】:2014-07-26 21:15:21
【问题描述】:

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

问题在于动态控件的事件不会触发 (ciDD_SelectedIndexChanged)。

按顺序:单击按钮后,它会为控件 ID 设置一个 Session 变量,创建控件,然后将它们添加到表格行中。这是通过 OnClick 完成的,而 OnInit 则用于回发。

有没有一种简单的方法可以为动态控件设置事件,还是我会以一种可怕的方式来解决这个问题?

按钮及其事件:

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

Row创建函数,对应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);
    }

【问题讨论】:

    标签: c# asp.net dynamic controls


    【解决方案1】:

    我发现在 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);
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2012-03-18
      • 2017-07-22
      • 1970-01-01
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-28
      相关资源
      最近更新 更多