Check/Uncheck checkboxes in GridView using JavaScript

The question regarding how to check/uncheck CheckBoxes within a GridView control using JavaScript has been asked many times. Here is a quick reference you can follow.

First we have the .aspx markup.

<script type="text/javascript">

    function SelectAll(id) {

        var frm = document.forms[0];

        for (i=0;i<frm.elements.length;i++) {

            if (frm.elements[i].type == "checkbox") {

                frm.elements[i].checked = document.getElementById(id).checked;

            }

        }

    } 

</script>

<!-- assuming that SqlDataSource1 is the datasource for my GridView -->

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" Width="400px">

    <Columns>

        <asp:TemplateField>

            <AlternatingItemTemplate>

                <asp:CheckBox ID="CheckBox1" runat="server" />

            </AlternatingItemTemplate>

            <ItemTemplate>

                <asp:CheckBox ID="CheckBox1" runat="server" />

            </ItemTemplate>

            <HeaderTemplate>

                <asp:CheckBox ID="cbSelectAll" runat="server" Text="Select All" />

            </HeaderTemplate>

            <HeaderStyle HorizontalAlign="Left" />

            <ItemStyle HorizontalAlign="Left" />

        </asp:TemplateField>

    </Columns>

</asp:GridView>

Next we have the code-behind in both VB and C#

VB

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

    If (e.Row.RowType = DataControlRowType.Header) Then

        'adding an attribute for onclick event on the check box in the header

        'and passing the ClientID of the Select All checkbox

        DirectCast(e.Row.FindControl("cbSelectAll"), CheckBox).Attributes.Add("onclick", "javascript:SelectAll('" & _

            DirectCast(e.Row.FindControl("cbSelectAll"), CheckBox).ClientID & "')")

    End If

End Sub

C#

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) {

    if (e.Row.RowType == DataControlRowType.Header) {

        //adding an attribute for onclick event on the check box in the header

        //and passing the ClientID of the Select All checkbox

        ((CheckBox)e.Row.FindControl("cbSelectAll")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("cbSelectAll")).ClientID + "')");

    }

}

The example above is fantastic, but there are a couple things that could be improved.

  1. The JavaScript Pseudo Protocol (Javascript:your method here) should be avoided, it's a fragment from the old Netscape days. Today there are better alternatives
  2. In this case we probably don't need the server-side portion altogether.

An excerpt about the JavaScript Pseudo Protocol:

"The javascript: pseudo-protocol should not be used in event handlers like onclick. It should only be used in attributes that contain a URL, for example in the href attribute of <a> elements and the action attribute of <form> elements. You can also use it to make bookmarlets." - Common JavaScript Mistakes

Another solution the .aspx markup: 

<script type="text/javascript">

    // Let's use a lowercase function name to keep with JavaScript conventions

    function selectAll(involker) {

        // Since ASP.NET checkboxes are really HTML input elements

        //  let's get all the inputs

        var inputElements = document.getElementsByTagName('input');

 

        for (var i = 0 ; i < inputElements.length ; i++) {

            var myElement = inputElements[i];

 

            // Filter through the input types looking for checkboxes

            if (myElement.type === "checkbox") {

 

               // Use the involker (our calling element) as the reference 

               //  for our checkbox status

                myElement.checked = involker.checked;

            }

        }

    } 

</script>

 

<!-- assuming that SqlDataSource1 is the datasource for my GridView -->

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">

    <Columns>

        <asp:TemplateField>

            <AlternatingItemTemplate>

                <asp:CheckBox ID="CheckBox1" runat="server" />

            </AlternatingItemTemplate>

            <ItemTemplate>

                <asp:CheckBox ID="CheckBox1" runat="server" />

            </ItemTemplate>

            <HeaderTemplate>

                <asp:CheckBox ID="cbSelectAll" runat="server" Text="Select All"                                   OnClick="selectAll(this)" />

            </HeaderTemplate>

            <HeaderStyle HorizontalAlign="Left" />

            <ItemStyle HorizontalAlign="Left" />

        </asp:TemplateField>

    </Columns>

</asp:GridView>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uncheck方法是在Java编程中,用于对方法进行排序的一种方式。collections.sort方法是Java集合框架中用于对集合进行排序的方法。当我们在使用该方法时,可以使用uncheck方法来忽略排序时可能发生的异常。 使用uncheck方法的好处是可以简化代码的编写和阅读。通常情况下,当我们对集合进行排序时,需要处理一些可能抛出的异常,比如NullPointerException或ClassCastException。而使用uncheck方法后,可以将这些异常隐藏起来,使得代码看起来更简洁。 uncheck方法的实现原理是利用了Java中的Lambda表达式和函数式接口。通过Lambda表达式,我们可以传入一个方法作为参数,然后使用函数式接口中的默认方法将这个方法进行包装,从而捕获在排序过程中可能发生的异常。这样,就不需要我们手动处理异常了。 另外,使用uncheck方法也能提高代码的可读性和可维护性。在代码中使用uncheck方法后,其他开发人员可以更轻松地理解我们的意图,并且在需要修改排序逻辑时,也更容易进行代码的维护。 需要注意的是,在使用uncheck方法时,我们需要确保在排序之前已经对集合进行了空值或类型的判断,以避免可能出现的空指针异常或类型转换异常。 总之,uncheck方法是一种简化代码和隐藏异常的方式,可以提高代码的可读性和可维护性。通过使用uncheck方法,我们可以更专注地实现业务逻辑,同时避免了繁琐的异常处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值