2011-04-21 23 views
-2

可能重复:
Looping through DataGrid rows and Checking Checkbox Control循环通过数据网格的行和检查复选框控件

我现在有一个GridView,其从学生表显示数据,有在GridView两个模板字段:复选框和标签(标签模板字段显示StudentID)。我在页面上有一个按钮,当用户点击按钮时,我需要遍历GridView中的每一行,然后找到复选框,然后我需要检查复选框是否被选中。如果复选框被选中,我需要将标签模板字段中的值添加到数据库中的其他表格中。我如何去实现这个目标?我正在使用C#代码。

+3

为什么你再次问的http://stackoverflow.com/questions/5743099/looping-through-datagrid-rows-and-checking-checkbox-control,提出您的疑惑同样的问题 – V4Vendetta 2011-04-21 11:37:52

回答

0

实际上,您可以使用OnItemCommand获取此信息。绑定ID到CommandArgument

protected void GVSearchResults_RowCommand(object sender, GridViewCommandEventArgs e) 
     { 

      foreach (GridViewRow gvr in gvSearchResults.Rows) 
      { 
       if (gvr.RowType == DataControlRowType.DataRow) 
       { 
        if (((Label)gvr.FindControl("lblStudentID")).Text == e.CommandArgument.ToString()) 
        { 
         bool isChecked = ((CheckBox)gvr.FindControl("cbStudent")).Checked; 
         int count = 0; 
         if (e.CommandName == "Save") 
         { 
          this.SaveStudentcheck(int.Parse(e.CommandArgument.ToString())); 
         } 
         break; 
        } 
       } 
      } 
      gvSearchResults.DataBind();    
     } 
相关问题