ali raza

C#, .Net ,Asp.net and my thoughts

how to check all check boxes on gridview in asp.net using javascript

please paste the following code in your java script area

function CheckAllDataViewCheckBoxes( checkVal)

{ for(i = 0; i < document.forms[0].elements.length; i++)

{elm = document.forms[0].elements[i]if (elm.type == ‘checkbox’)

{

{elm.checked = checkVal }

}

}

}

and following line on the header for ur check boxes field in item tamplate of grid view

<input id=’chkAllItems’ type=’checkbox’ onclick=’CheckAllDataGridCheckBoxes(document.forms[0].chkAllItems.checked)’ />

all sample Grid View code is here

<asp:GridView ID=”grdMembers” AllowPaging=”true” AllowSorting=”true” runat=”server” Width=”100%” AutoGenerateColumns=”False” OnRowDataBound=”grdMembers_RowDataBound” CellPadding=”3″ >

<HeaderStyle Height=”25px” ></HeaderStyle>
<Columns>
<asp:TemplateField ItemStyle-HorizontalAlign=”Center” headerText=”<input id=’chkAllItems’ type=’checkbox’ onclick=’CheckAllDataViewCheckBoxes(document.forms[0].chkAllItems.checked)’ />” >
<ItemTemplate>
<input id=”RowSelectorUser” runat=”server” name=”RowSelectorUser” type=checkbox value='<%# Eval(“ItemId”) %>’ />

</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

on run time when u click check box on gridview all check boxes checked or unchecked with it

6 responses to “how to check all check boxes on gridview in asp.net using javascript

  1. BURHAN August 22, 2007 at 10:01 am

    bhai this will check all the check box in the page not complete
    solution of the problem???????

  2. nisar October 5, 2007 at 11:37 am

    Thanks a lot….

  3. Tran Xuan Thang October 13, 2008 at 2:18 am

    I think that if we check the name of checkbox in javascript, we can solve this problem:

    function CheckAllDataViewCheckBoxes(checkVal)
    {
    for(i=0;i14) //14 is length of checkbox name (‘chkCheckSelect’)
    {
    alert(elmName.length);
    elmName = elmName.substring(elmName.length – 14,elm.length);
    alert(elmName);
    if(elmName == ‘chkCheckSelect’)//name of checkbox
    elm.checked = checkVal;
    }
    }

    }

    //In gridview

    <asp:TemplateField HeaderText=””>

    <asp:CheckBox ID=”CheckBox1″ runat=”server” Checked=” />

    <asp:CheckBox ID=”chkCheckSelect” runat=”server” Checked=” Enabled=”true” />

  4. Tran Xuan Thang October 13, 2008 at 2:40 am

    JAVASCRIPT:

    function CheckAllDataViewCheckBoxes(checkVal)
    {
    for(i=0;i14) //14: length of ‘chkCheckSelecl’
    {
    alert(elmName.length);
    elmName = elmName.substring(elmName.length – 14,elm.length);
    alert(elmName);
    if(elmName == ‘chkCheckSelect’)
    elm.checked = checkVal;
    }
    }

    }

    ASP.Net page

    <asp:TemplateField HeaderText=””>

    <asp:CheckBox ID=”CheckBox1″ runat=”server” Checked=” />

    <asp:CheckBox ID=”chkCheckSelect” runat=”server” Checked=” Enabled=”true” />

    <asp:TextBox ID=”TextBox1″ runat=”server” Text=”>

    <asp:Label ID=”lblID” runat=”server” Text=”>

  5. Srishti July 17, 2009 at 7:01 pm

    thanks a lot…..very helpful post !!

  6. Ali Adravi September 8, 2009 at 11:51 am

    In place of :
    onclick=’CheckAllDataGridCheckBoxes(document.forms[0].chkAllItems.checked)’

    Use :
    onclick=’CheckAllDataGridCheckBoxes(this.checked)’

    Hope in future it will help you.

    Thanks
    Ali Adrvi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: