5

I have a gridview where the checkboxes start off disabled. I want to enable them when I click the edit button that's also in the gridview. Here's 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 then here's the code that I'm trying to use. Basically, when I hit the edit button, I want the checkboxes themselves to be enabled. For whatever reason, the checkbox isn't enabled at all when the page loads back up. I just started off trying to enable "Checkbox1" after the edit button is clicked but eventually 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
  • What's happening with the code you're using? Commented Sep 9, 2011 at 15:48
  • Doesn't this code enable the checkbox? Could you be a more clear on the question. Commented Sep 9, 2011 at 15:50
  • and what is the question? what does not work? if you debug, do you manage to reach this code line: CheckBox chk = (CheckBox)row.FindControl("CheckBox1"); ? Commented Sep 9, 2011 at 15:50
  • Sorry, I made a slight edit to the question. Davide, I get past that line of code with no problem and there's no errors in compilation. The problem is that the checkbox doesn't enable when the page reloads. Commented Sep 9, 2011 at 16:14

2 Answers 2

4

If you want the Edit control to be different than the standard control, you should use the "EditItemTemplate". This will allow the edit row to have different controls, values, etc... when the row's mode changes.

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>
Sign up to request clarification or add additional context in comments.

1 Comment

@Zachary thank you so much!! I've been searching for this for AGES now, kept it nice and simple!
1

I guess you could loop through all the rows of the GridView and enable the checkboxes something like 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 Comment

right, that was what I was gonna do but using edititemtemplate was just a much easier way of doing it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.