2012-03-30 53 views
0

我有以下代碼:使動態創建下拉列表公開的其他類

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


        } 

我有一個下拉列表,當我爲每個選項選擇的選項中選擇一個行創建了包含選定值,另一列有一個dropdownlist.I要爲該下拉列表創建動態添加一個selectedindexchanged.I請求如何在我selectedindexchange中將引用更改爲動態創建的下拉列表,並將其命名爲「drop」。我試圖讓它公開,但不是我的表格不是很好創建。它是一個使用c#的as.net web應用程序。

回答

3

SelectedIndexChanged事件中,您可以訪問觸發事件的對象。在你的情況下,它將是你關聯事件的動態創建的控件。您可以在事件中鍵入強制類型爲DropDownList,如下所示:

protected void drop_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    var drop = (DropDownList)sender; 
    if (drop.SelectedValue == "1") 
    { 
     ... 
    } 
} 
+0

sender是動態創建的下拉列表的名稱? – Bibu 2012-03-30 14:10:45

+0

'sender'是觸發事件的對象。在你的情況下,它將是你關聯到'drop_SelectedIndexChanged'方法的動態創建的DropDownList。即你的線'drop.SelectedIndexChanged + =新的EventHandler(drop_SelectedIndexChanged);' – CAbbott 2012-03-30 14:12:37

+0

所以我dropdownlist的名稱下降...如果我寫var drop1 =(DropDownList)下降 - > drop不被識別 – Bibu 2012-03-30 14:16:48