Possible Duplicate:
Looping through Da开发者_Python百科taGrid rows and Checking Checkbox Control
I currently have a GridView which displays data from a Student Table, there is two template fields in the gridview: Checkbox and Label (the label template field displays the StudentID). I have a button on the page which when the user clicks the button I need to loop through each row in the GridView, then find the CheckBox then I need to check if the checkbox is checked or not. If the checkbox is checked I need to add the value in the Label Template Field to a different table in the database. How do I go about acheiving this? I am using C# Code.
Actually you can get this information using the OnItemCommand. Bind the ID to the 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();
}
精彩评论