Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Gridview.

On the item template I have a checkbox. Now I want the checkbox should be checked whenever I clicked on the each and every row of the gridview.

Below is the source code:
XML
<asp:GridView ID="gvEmpDetails" runat="server" AutoGenerateColumns="false"
           onrowdatabound="gvEmpDetails_RowDataBound">
         <Columns>

         <asp:TemplateField HeaderText="SelectAll">
         <ItemTemplate>
         <asp:CheckBox ID="cbCheck" runat="server" />

         </ItemTemplate>
         </asp:TemplateField>
        <asp:BoundField DataField="Empid" HeaderText="Id" />
           <asp:BoundField DataField="Name" HeaderText="Name" />
           <asp:BoundField DataField="Address" HeaderText="Address" />
           <asp:BoundField DataField="Salary" HeaderText="Salary" />
           <asp:TemplateField HeaderText="Edit"></asp:TemplateField>
           <asp:TemplateField HeaderText="Delete"></asp:TemplateField>
       </Columns>
       </asp:GridView>


In the row databound event i wrote the below code:
C#
if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (GridViewRow row in gvEmpDetails.Rows)
            {
                CheckBox chk = (CheckBox)e.Row.FindControl("cbCheck");

                e.row.Attributes.Add("OnClick", );
            }

        }

Not finding any way.
Please help me
Posted
Updated 18-Mar-11 19:21pm
v2

1 solution

Very easily using jQuery. Here: Quick Tip: Click Table Row to Trigger a Checkbox Click[^]


Now, your way:
C#
if (e.Row.RowType == DataControlRowType.DataRow)
{
     foreach (GridViewRow row in gvEmpDetails.Rows)
     {
         CheckBox chk = (CheckBox)e.Row.FindControl("cbCheck");
         e.row.Attributes.Add("OnClick", "RowClicked("+ chk.ClientID +")");
     }
}

// Then in Javascript <code>RowClicked</code> method, 
// just set the checked property of the checkbox as desired 
// or call click event of checkbox or anything you feel like.
 
Share this answer
 
Comments
Hiren solanki 19-Mar-11 2:22am    
Good answer sandeep.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900