2012-07-09 61 views
0

我想要在GridView中獲得CheckBox的值。我可以使用事件RowDataBound來綁定GridView。但在ButtonClick事件中,即使我檢查了CheckBoxCheckBox的值也始終爲false。如何使用C#獲取gridview複選框的值?

代碼背後:

protected void GridView1_RowDatabound(object sender, GridViewRowEventArgs e) 
    { 

     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      DropDownList dddate = (DropDownList)e.Row.FindControl("DdlDate"); 
      DropDownList ddYear = (DropDownList)e.Row.FindControl("DdlYear"); 
      for (int i = System.DateTime.Now.Year; i > (System.DateTime.Now.Year) - 100; i--) 
       ddYear.Items.Add(Convert.ToString(i)); 
      for (int i = 1; i < 32; i++) 
       dddate.Items.Add(Convert.ToString(i)); 
     } 
    } 
    protected void btnRetrieveCheck_Click(object sender, EventArgs e) 
    { 
     foreach (GridViewRow row in GridView1.Rows) 
     { 
      CheckBox cb = (CheckBox)row.FindControl("chkSel"); 
      if (cb != null && cb.Checked) 
      { 

       DropDownList dddate = (DropDownList)row.FindControl("DdlDate");    //Bind data to the GridView control. 
       DropDownList ddYear = (DropDownList)row.FindControl("DdlYear"); 
       DropDownList ddmonth = (DropDownList)row.FindControl("DdlMonth"); 

      } 
     } 
    } 
+0

是您的'chkSel'動態創建或設計時間? – dotNETbeginner 2012-07-09 08:46:14

回答

0

你應該掛接到該複選框,而不是單擊事件經過事件。

你的問題不是很清楚。你可以張貼更多的代碼嗎?例如,什麼是btnRetrieveCheck - 它是實際的複選框嗎?請使用您的XAML更新問題,以便我們可以看到有什麼綁定。

+0

我有一個gridview有幾列和一個包含複選框的templatefield。當用戶點擊複選框時,我想用1(如果選中)或0(如果未選中)更新數據庫。這就是爲什麼我使用btnRetrieveCheck – Parthi 2012-07-09 06:56:32

+0

你可以更新你的問題,包括DataGrid的xaml。我還是不明白。另外我的評論重新EndEdit適用於WinForms,所以我會刪除它。 – Michael 2012-07-09 07:08:33