如何检查Gridview的rowclick上的复选框? [英] How to check the checkbox on rowclick of Gridview?

查看:132
本文介绍了如何检查Gridview的rowclick上的复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Gridview.

在项目模板上,我有一个复选框.现在,我希望每次单击gridview的每一行时都应选中该复选框.

以下是源代码:

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:

<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:

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

推荐答案

非常容易使用jQuery.此处:快速提示:单击表行以触发复选框,点击 [
Very easily using jQuery. Here: Quick Tip: Click Table Row to Trigger a Checkbox Click[^]


Now, your way:
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.


这篇关于如何检查Gridview的rowclick上的复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆