CheckBox Gridview Enable and Disable

I have a gridview where the checkboxes start to turn off. I want to enable them when I click the edit button, which also appears in gridview. Here is the markup

<asp:GridView ID="grd_Bookcode" runat="server" DataSourceID="sqldatasource1" autogeneratecolumns="False" onrowcommand="grd_Bookcode_RowCommand1" onrowdatabound="grd_Bookcode_RowDataBound"> <Columns> <asp:BoundField DataField="BookCode" HeaderText="Book Code"/> <asp:BoundField DataField="mag_name" HeaderText="Name"/> <asp:BoundField DataField="display_date" HeaderText="Display Date"/> <asp:TemplateField HeaderText = "PC"> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("82_PC").ToString() == "1" ? true:false %>' Enabled="false" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="eReader"> <ItemTemplate> <asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# Eval("83_eReader").ToString() == "1" ? true:false %>' Enabled="false" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Tablet"> <ItemTemplate> <asp:CheckBox ID="CheckBox3" runat="server" Checked='<%# Eval("84_Tablet").ToString() == "1" ? true:false %>' Enabled="false"/> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Mobile"> <ItemTemplate> <asp:CheckBox ID="CheckBox4" runat="server" Checked='<%# Eval("85_Mobile").ToString() == "1" ? true:false %>' Enabled="false" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="None"> <ItemTemplate> <asp:CheckBox ID="CheckBox5" runat="server" Checked='<%# Eval("86_None").ToString() == "1" ? true:false %>' Enabled="false" /> </ItemTemplate> </asp:TemplateField> <asp:CommandField ShowEditButton="True" /> </Columns> 

And here is the code I'm trying to use. Basically, when I click on the edit button, I want the checkboxes to be on. For some reason, the checkbox is not enabled at all when the page loads. I just started by trying to enable "Checkbox1" after clicking the edit button, but in the end I want to enable all 5 checkboxes.

  protected void grd_Bookcode_RowCommand1(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Edit") { int index = Convert.ToInt32(e.CommandArgument); GridViewRow row = grd_Bookcode.Rows[index]; CheckBox chk = (CheckBox)row.FindControl("CheckBox1"); chk.Enabled = true; } } 
+4
source share
2 answers

If you want the Edit control to be different from the standard control, you should use the "EditItemTemplate". This will allow the edit line to have different controls, values, etc .... when changing the line mode.

Example:

  <Columns> <asp:TemplateField HeaderText="PC"> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("82_PC").ToString() == "1" ? true:false %>' Enabled="false" /> </ItemTemplate> <EditItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" Checked="true" Enabled="false" /> </EditItemTemplate> </asp:TemplateField> </Columns> 
+3
source

I think you could skip all the rows of the GridView and enable the checkboxes as shown below:

  protected void grd_Bookcode_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Edit") { for (int index = 0; index < GridView1.Rows.Count; index++) { CheckBox chk = grd_Bookcode.Rows[index].FindControl("CheckBox" + index + 1) as CheckBox; chk.Enabled = true; } } } 

Hope this helps!

+1
source

All Articles