Reputation: 811
I have an ASP GridView with a column that contains CheckBox controls. These CheckBoxes are bound to a bit field accessed via a SqlDataSource. The GridView is editable and the CheckBoxes are disabled on load. I am trying to get the enabled property to change to true during edit, so the CheckBox in the row that is being edited can be changed, and this change updated to the bit field in the database.
The ASP code
<asp:UpdatePanel ID="upGridView" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gvChecklist" runat="server"
AutoGenerateColumns="false" DataSourceID="dsChecklist"
AutoGenerateEditButton="true" onrowupdating="gvChecklist_RowUpdating"
onrowediting="gvChecklist_RowEditing">
<Columns>
<asp:TemplateField HeaderText="Finished">
<ItemTemplate><asp:CheckBox ID="cbFinished" runat="server" Enabled="false" Checked='<%# Eval("Finished") ?? false %>' /> </ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Division"
HeaderText="Division"
readonly="true" />
<asp:BoundField DataField="Application"
HeaderText="Application"
readonly="true" />
<asp:BoundField DataField="Task"
HeaderText="Task"
readonly="true" />
<asp:BoundField DataField="TestedBy" HeaderText="Tested By" readonly="true"/>
<asp:BoundField DataField="Notes" HeaderText="Notes" ReadOnly="false"/>
<asp:BoundField DataField="JiraTicket"
HeaderText="JIRA Ticket"
readonly="false" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
C# Code Behind for the RowEditing event
protected void gvChecklist_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView gvChecklist = (GridView)LoginView1.FindControl("gvChecklist");
CheckBox cb = (CheckBox)gvChecklist.Rows[e.NewEditIndex].Cells[1].FindControl("cbFinished");
cb.Enabled = true;
gvChecklist.DataBind();
}
-edit: I felt the need to add a little more information here. The code to set the CheckBox cb to the GridView Row's CheckBox seems to be working correctly, as I can see the enabled property = true through the debugger. I am wondering if it is getting set back to false due to PostBack after this event runs. The Load method for this page is empty, but I am thinking that the enabled=false code is being hit in the ASP page.
UI of GridView
Upvotes: 1
Views: 1542
Reputation: 811
The solution for this was surprisingly simple. Instead of putting a CheckBox in a TemplateField, I was able to set a DataField property for the CheckBox column. This allowed the edit functionality to work with no further C# code.
<asp:CheckBoxField DataField="Finished"
HeaderText="Finished"
readonly="false" />
Upvotes: 1