[英]ASP.NET Dynamically created controls in C#: failed events
我正在创建一个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);
}
行创建功能及其对应的列表:
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);
}
我发现在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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.