2012-04-09 106 views
0

我創造了一個循環的表ASP頁這樣分配給TD表中的值:從下拉列表

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個以上的單元格。現在,我要爲我創造,使每個something.So下拉列表如果我選擇1第一行的東西會出現,如果我選擇1另一行別的東西就會出現。我做了一個selectedindexchangedchanged,但如果我從第一行中選擇1,它將顯示兩行。我如何分別引用我在循環中創建的每個下拉列表?我使用Asp.Net Web應用程序用C#

回答

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

希望這將適用於你......幸福......編碼

+0

哪能在使用的SelectedIndexChanged分配給celula.RowSpan從下拉列表的值Δα – Bibu 2012-04-09 12:13:32

+0

u能請解釋在下拉菜單中選擇指標更改事件確切的功能? – 2012-04-09 12:17:44

+0

所以我有那些下拉每一行創建的列表。例如,我有3行3下拉列表。如果我從第一個下拉列表中選擇值1,那麼我希望這個值分配給celula.RowSpan,所以它會是這樣的: – Bibu 2012-04-09 12:29:20