如何使用C#获取gridview中复选框的值?

时间:2012-07-09 06:21:10

标签: c# wpf gridview checkbox

我想在CheckBox中获得GridView的值。我可以使用事件GridView绑定RowDataBound。但在Click Button事件CheckBox事件中CheckBox的值始终为假,即使我选中了 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"); } } }

代码背后:

{{1}}

1 个答案:

答案 0 :(得分:0)

您应该选中复选框上的Checked事件而不是click事件。

你的问题不是很清楚。你可以发布更多的代码吗?例如,什么是btnRetrieveCheck - 它是实际的复选框吗?请使用您的XAML更新问题,以便我们了解具体的绑定。