2011-11-17 26 views
0

我正在使用VS2010,C#,我有一個表,其數據應該動態創建(從SQL服務器表),我必須添加一個組合框(3項)到其中之一列,這個組合框也是動態創建的,然後我給每個組合框一個唯一的ID,它有autopost回設置關閉,並且enableviewstate和viewstatemode爲true並啓用,當用戶更改某些組合框的值(每行有一個combox),然後按下提交按鈕,我想有我的組合框的當前狀態,但他們選擇的索引是0,所以我不能使用它們,我該怎麼辦?我有什麼選擇? (我發現使用的FindControl和組合框的唯一ID每個組合框)查找動態創建的組合框的值

感謝

+0

你在哪裏使用你的組合框。在頁面或GridView? –

+0

我在我的表格中創建組合框,表格行是動態創建的,並且每個單元格中都添加了3個組合框。 –

回答

1

請在下面找到答案的上述問題

  1. 首先,你需要註冊組合框在Javascript onchange事件同時創建動態組合框。
  2. 將一個隱藏字段放在頁面上
  3. 然後把代碼放在onchange事件中,使用clientid在onchange事件中設置隱藏字段中的值,然後從服務器端獲得隱藏字段的值。
+0

感謝rahul,請給我一些關於實現過程的示例代碼? –

+0

請你按照上述步驟,讓我知道是否有任何錯誤或問題?這將是很好,你給我錯誤消息,所以我可以diffenately解決你的問題。我希望你明白。目前我沒有相同的代碼。但我已經嘗試了上面的代碼,它爲我工作。 –

0

可以使用回傳..這裏是一個示例代碼段..如果你想在回傳工作,然後再按照這個..否則你可以採取的辦法是拉胡爾告訴你..

public partial class DynamicCombo : System.Web.UI.Page 
    { 
     DropDownList list; 

     protected void Page_Init(object sender, EventArgs e) 
     { 
      Table table = CreateHtmlTable(); 
      list = new DropDownList(); 
      list.AutoPostBack = true; 
      list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged); 
      list.ID = "cbo"; 
      list.Items.Add(new ListItem("value1", "1")); 
      list.Items.Add(new ListItem("value2", "2")); 
      list.Items.Add(new ListItem("value3", "3")); 
      table.Rows[0].Cells[0].Controls.Add(list); 

      pnl.Controls.Add(table); 
     } 

     private void list_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      Response.Write("<script>alert(\"" + list.SelectedIndex + "\");</script>"); 
     } 

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

     private Table CreateHtmlTable() 
     { 
      Table table = new Table(); 
      table.Rows.Add(new TableRow()); 
      table.Rows[0].Cells.AddRange(new TableCell[] { new TableCell(), 
                 new TableCell(), 
                 new TableCell()}); 
      return table; 
     } 
    }