Questions › How to enable a checkbox in Gridview only when it's in the Edit mode?
Leon Asked

This is how I have my checkbox column currently:

          <asp:TemplateField HeaderText="Hide/Show" ItemStyle-HorizontalAlign="Center" SortExpression="Hide_Show">
                <ItemTemplate>
                    <asp:CheckBox ID="HideShowChk" runat="server" Checked='<%# Bind("Hide_Show") %>' />
                </ItemTemplate>
            </asp:TemplateField>

I have no code in my code behind because it's not necessary. Basically, a user can check or uncheck it, but it won't be reflected in the database unless I actually go into Edit mode, check or uncheck it, and then hit update so that the changes ARE reflected in the database.

Is there a way to have it so that the checkbox is "disabled" or anything of that sort while it's not in edit mode?


1 Answers :
Navajyoth CS answered

You have to use edititemtemplate for enabling it in the edit mode

 <edititemtemplate>
 <asp:CheckBox ID="HideShowChk" runat="server" Checked='<%# Bind("Hide_Show") %>' />
 </edititemtemplate>

If you still want to use the check box inside the ItemTemplate make it disable using CSS or jQuery.

Leon replied
That did the trick, thanks. However, the column now shows nothing. Thoughts?;
Navajyoth CS replied
put it in the <ItemTemplate> also and make it disabled something like <asp:CheckBox ID="CheckBox1" runat="server" Enabled="false" />;
Leon replied
That's that, thank you.;