我有以下代码:
foreach (ListItem item in check.Items)
{
if (item.Selected)
{
TableRow row = new TableRow();
row.Style.Add("color", "white");
TableCell cell = new TableCell();
cell.Style.Add("background-color", "blue");
Label lbl = new Label();
lbl.Text = item.Text;
cell.Controls.Add(lbl);
row.Cells.Add(cell);
TableCell cell1 = new TableCell();
cell1.Style.Add("background-color", "green");
DropDownList drop = new DropDownList();
drop.Style.Add("align", "right");
drop.Items.Add(" ");
drop.Items.Add("1");
drop.Items.Add("2");
drop.DataValueField = "0";
drop.DataValueField = "1";
drop.DataValueField = "2";
cell1.Controls.Add(drop);
row.Cells.Add(cell1);
this.TblCheck.Rows.Add(row);
drop.SelectedIndexChanged += new EventHandler(drop_SelectedIndexChanged);
drop.AutoPostBack = true;
}
}
}
private void drop_SelectedIndexChanged(object sender,EventArgs e)
{
if (drop.SelectedValue == "1")
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.Style.Add("background-color", "blue");
Label lbl = new Label();
lbl.Text = "H1";
cell.Controls.Add(lbl);
row.Cells.Add(cell);
this.tabel1.Rows.Add(row);
}
if (drop.SelectedValue == "2")
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.Style.Add("background-color", "blue");
Label lbl = new Label();
lbl.Text = "H1";
cell.Controls.Add(lbl);
row.Cells.Add(cell);
TableRow row1 = new TableRow();
TableCell cell1 = new TableCell();
cell1.Style.Add("background-color", "blue");
Label lbl1 = new Label();
lbl1.Text = "H2";
cell1.Controls.Add(lbl1);
row1.Cells.Add(cell1);
this.tabel1.Rows.Add(row);
this.tabel1.Rows.Add(row1);
}
}
我有一个下拉列表,当我为每个选项选择一个选项时,会创建一个包含所选值的行,另一个列包含一个下拉列表。我希望为创建的动态下拉列表添加一个selectedindexchanged.I请问如何在我的selectedindexchanged中引用动态创建的下拉列表,其名称为“drop”。我试图将其公开但我的表格创建得不好。这是一个使用c#的as.net Web应用程序。
答案 0 :(得分:3)
在SelectedIndexChanged事件中,您可以访问触发事件的对象。在您的情况下,它将是您将事件关联到的动态创建的控件。您可以在事件中键入cast作为DropDownList,如下所示:
protected void drop_SelectedIndexChanged(object sender, EventArgs e)
{
var drop = (DropDownList)sender;
if (drop.SelectedValue == "1")
{
...
}
}