0

我在这样的asp页面中循环创建一个表:

   foreach (ListItem item in check.Items)
            {
                if (item.Selected)
                {

                    DropDownList drop = new DropDownList();

                    drop.Items.Add("1");
                    drop.Items.Add("2");

                    drop.DataValueField = "1";
                    drop.DataValueField = "2";





                    TableRow row = new TableRow();
                    TableCell celula = new TableCell();
                    celula.Style.Add("width", "200px");
                    celula.Style.Add("background-color", "red");

                    celula.RowSpan = 2;
                    celula.Text = item.Value;
                    TableCell celula1 = new TableCell();
                    celula1.Style.Add("background-color", "green");
                    celula1.Style.Add("width", "200px");

                    celula1.RowSpan = 2;

                    TableCell celula2 = new TableCell();
                    celula2.Style.Add("width", "200px");
                    celula2.Style.Add("background-color", "blue");
                    celula2.Text = "unu";
                    TableRow row1 = new TableRow();
                    TableCell cel = new TableCell();
                    cel.Text = "lalala";
                    cel.Style.Add("background-color", "brown");




                    celula1.Controls.Add(drop);
                    row.Cells.Add(celula);
                    row.Cells.Add(celula1);
                    row.Cells.Add(celula2);
                    row1.Cells.Add(cel);
                    this.tabel.Rows.Add(row);
                    this.tabel.Rows.Add(row1);



                }
            }

对于我检查的每个值,都会创建一个新行,其中包含选中复选框的值、一个下拉列表和另外 2 个单元格。现在我想为我创建的每个下拉列表制作一些东西。因此,如果我为第一行选择 1,则会出现某些内容,如果我为另一行选择 1,则会出现其他内容。我做了一个 selectedindexchanged 但如果我从第一行中选择 1 它将显示在两行中。我如何分别参考我在循环中创建的每个下拉列表?我正在使用带有 c# 的 Asp.Net Web 应用程序

4

1 回答 1

0

如下修改您的代码。

protected void Page_Load(object sender, EventArgs e)
{        
    LoadData();
}   

定义 LoadData 函数如下

 private void LoadData()
 {
     foreach (ListItem item in check.Items)
     {
         if (item.Selected)
         {

             DropDownList drop = new DropDownList();

             drop.Items.Add("1");
             drop.Items.Add("2");

             drop.DataValueField = "1";
             drop.DataValueField = "2";
             drop.AutoPostBack = true;

             // assign id property to dropdown.

             drop.ID = "ddl_" + item.value.ToString();

             // add event handled for dynamically generated dropdown      
             drop.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);



             TableRow row = new TableRow();
             TableCell celula = new TableCell();

             // assing ID property to cell.

             celula.ID = "celula_" + item.value.ToString();
             celula.Style.Add("width", "200px");
             celula.Style.Add("background-color", "red");

             celula.RowSpan = 2;
             celula.Text = "item " + item.Value.ToString();
             TableCell celula1 = new TableCell();
             celula1.Style.Add("background-color", "green");
             celula1.Style.Add("width", "200px");

             celula1.RowSpan = 2;

             TableCell celula2 = new TableCell();
             celula2.Style.Add("width", "200px");
             celula2.Style.Add("background-color", "blue");
             celula2.Text = "unu";
             TableRow row1 = new TableRow();
             TableCell cel = new TableCell();
             cel.Text = "lalala";
             cel.Style.Add("background-color", "brown");




             celula1.Controls.Add(drop);
             row.Cells.Add(celula);
             row.Cells.Add(celula1);
             row.Cells.Add(celula2);
             row1.Cells.Add(cel);
             this.tabel.Rows.Add(row);
             this.tabel.Rows.Add(row1);
         }
     }

    }

如下处理事件

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList dd = (DropDownList)sender;

    // you can get dropdown from table row over here.
    // process with it as per your requirement.

    string strdropdownID = ((System.Web.UI.Control)(dd)).ID;

    string id = strdropdownID.Split('_')[1].ToString();
    string tblcID = "celula_" + id.ToString();
    TableCell celula = (TableCell)tabel.FindControl(tblcID);
    celula.RowSpan = Convert.ToInt32(dd.SelectedValue);
}

希望这对你有用......快乐的编码......

于 2012-04-09T11:38:28.050 回答