I have the following code:
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);
}
}
I have a drop down list and when I select an option for each option selected a row is created which contains the selected value and another column with a dropdownlist.I want for that drop down list created dynamic to add a selectedindexchanged.I ask how to make reference in my selectedindexchanged to the dynamic created dropdownlist whici is named “drop”.I tried to make it public but than my table isn’t created well.It’s an as.net web application using c#.
In the SelectedIndexChanged event you have access to the object which triggered the event. In your case it would be the dynamically created control to which you associated the event. You can type cast that as a DropDownList within the event like so: