Reputation: 2749
I have a gridview that has a template field containing a checkbox as one of the fields
<asp:TemplateField HeaderText="Confirm Driver Details" FooterText="Confirm Driver Details">
<ItemTemplate>
<asp:CheckBox ID="chkConfirmDetails" runat="server" MaxLength="100" Width="50px" Enabled="false" Checked="true"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
Initially this is checked and not enabled.
The user uploads a file which is loaded into an object and then bound to the gridview. On the RowDataBound event some validation is performed on the data. In certain cases if some of the validation fails I want to enable the checkbox and untick it.
CheckBox chkConfirmDetails = (CheckBox)e.Row.Cells[e.Row.Cells.Count - 2].FindControl("chkConfirmDetails");
//If this is enabled then we just need to check whether it is ticked or not
if (!chkConfirmDetails.Enabled)
{
if (!ValidateDriver(_uploadData.DriverData[e.Row.DataItemIndex], out driverValidationMessage))
{
Label label = new Label();
label.Text = "Details of this Driver cannot be found<br />Please double-check to see if the Driver Number, Date of Birth and PPSN are entered correctly<br />If you are sure you have the correct details entered tick this box to confirm";
e.Row.Cells[e.Row.Cells.Count - 2].Controls.Add(label);
e.Row.Cells[e.Row.Cells.Count - 2].BackColor = System.Drawing.Color.Yellow;
((TableRow)e.Row.Cells[e.Row.Cells.Count - 2].Parent).BackColor = System.Drawing.Color.Red;
chkConfirmDetails.Enabled = true;
chkConfirmDetails.Checked = false;
_numberOfInvalidCells++;
}
}
This all works fine, the checkbox becomes enabled and is unticked.
The user can then choose to tick the box to confirm that the details are correct and re-validate the row. On each row of the datagrid there is a button that the user can click to perform this function. So when the user clicks the validate row button the above code is called again. This time the checkbox is enabled and may or may not be ticked depending on what the user has done.
However when I get a reference to the checkbox by using the FindControl method it is still showing up as Not Enabled and Checked (even if it is enabled and Not Checked on the form)
Can someone explain why this is occurring and a way to get the actual state of the checkbox?
EDIT
Looking at the Request.Form.AllKeys data, it has keys for most of the other template items in the gridview (textboxes and dropDownLists) but not the checkbox or the validate button
Upvotes: 2
Views: 1660
Reputation: 460340
RowDataBound
is only called if you DataBind the GridView to it's DataSource.
That means you've called it before. But as a result all changes made by the user are overridden.
So the question is, why did you need to call GridView.DataBind()
fro revalidation? Can't you do this in a method that iterates all GridViewRows?
By the way, Is there a specific reason why you used
(CheckBox)e.Row.Cells[e.Row.Cells.Count - 2].FindControl("chkConfirmDetails")
instead of simply
(CheckBox)e.Row.FindControl("chkConfirmDetails")
?
Upvotes: 2