Reputation: 71
I am having an asp .Net page where i have a labe named rowId
protected void testResultsGridView_onRowUpdating(object sender, GridViewUpdateEventArgs e)
{
string str = string.Empty;
GridViewRow row = testResultsGridView.Rows[e.RowIndex];
Int32 id = Int32.Parse(((Label)row.FindControl("rowId")).Text);
String Cause = ((DropDownList)row.FindControl("Cause")).SelectedValue;
String comment = ((TextBox)row.FindControl("comment")).Text;
String check = ((TextBox)row.FindControl("check")).Text;
String reRunStatus = ((DropDownList)row.FindControl("Status")).SelectedValue;
String subCategory = ((DropDownList)row.FindControl("Category")).SelectedValue;
foreach (GridViewRow gvrow in testResultsGridView.Rows)
{
CheckBox chk = (CheckBox)gvrow.FindControl("cbSelect");
if (chk != null && chk.Checked)
{
id = Int32.Parse(((Label)gvrow.FindControl("rowId")).Text); ;
UpdateTestCase(id, rootCause, subCategory, comment, RTC, reRunStatus);
}
}
testResultsGridView.EditIndex = -1;
BindData();
}
In this case first find control of rowId return values and second one inside selected scheckbox returns null. I am not even adding any control dynamically. What might be the reason?
Upvotes: 1
Views: 939
Reputation: 148110
GridView not only have DataRows but has other row types like header, foooter. Your control are in DataRow type. You need to check if the row where you are trying to find rowId
is a DataRow
as there are other row types which would not have conntrol with id
rowId
You can find more about RowType
here. You have to find control where RowType
is DataRow
.
if(gvrow.RowType == DataControlRowType.DataRow)
Why it works for first time
It works for first time because the event onRowUpdating is fired on row having type DataRow.
Why it does not work for second time
For second time you are iterating the Rows of GridView which have other row types as well. If you debug the code you will see you will get null when RowType
is not DataRow
.
You code would be
foreach (GridViewRow gvrow in testResultsGridView.Rows)
{
if(gvrow.RowType == DataControlRowType.DataRow)
{
CheckBox chk = (CheckBox)gvrow.FindControl("cbSelect");
if (chk != null && chk.Checked)
{
id = Int32.Parse(((Label)gvrow.FindControl("rowId")).Text); ;
UpdateTestCase(id, rootCause, subCategory, comment, RTC, reRunStatus);
}
}
}
Upvotes: 2