Click here to Skip to main content
14,110,039 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
--scenario
<asp:GridView ID="GridZeroApprovedKm" AllowPaging="true" runat="server" AutoGenerateColumns="false"
            DataKeyNames="Address_id" OnRowCancelingEdit="GridZeroApprovedKm_RowCancelingEdit"
            OnRowDataBound="GridZeroApprovedKm_RowDataBound" OnRowDeleting="GridZeroApprovedKm_RowDeleting"
            OnRowEditing="GridZeroApprovedKm_RowEditing" OnRowUpdating="GridZeroApprovedKm_RowUpdating">
            <SelectedRowStyle CssClass="ResultSelectedRow" BackColor="Cyan" />
            <columns>
                <%-- hub_name,address_id,geocode_address--%>
                <asp:TemplateField HeaderText="Select All">
                    <HeaderTemplate>
                        <asp:CheckBox ID="chkb1" runat="server" Text="Select All" OnCheckedChanged="sellectAll"
                            AutoPostBack="true" />
                    </HeaderTemplate>
                     <itemstyle horizontalalign="Center" verticalalign="Middle" />
                    <itemtemplate>
                        <asp:CheckBox ID="chkb2" runat="server" Text="Select" />
                    </itemtemplate>
                    <itemstyle horizontalalign="center" />


---code behind
protected void sellectAll(object sender, EventArgs e)
        {
            CheckBox ChkBoxHeader = (CheckBox)GridZeroApprovedKm.HeaderRow.FindControl("chkb1");
            foreach (GridViewRow row in GridZeroApprovedKm.Rows)
            {
                CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkb2");
                if (ChkBoxHeader.Checked == true)
                {
                    ChkBoxRows.Checked = true;
                }
                else
                {
                    ChkBoxRows.Checked = false;
                }
            }
        }


--when i click the header checkbox
--it checks all the checkbox of the gridview....but the page is refreshed....i have made the autopostback property as true. Why does it refresh ?
Posted
Updated 11-Sep-13 6:35am
v2
Comments
Richard C Bishop 11-Sep-13 12:36pm
   
That is what the autopostback property does.
Anand Kumar Prajapati 11-Sep-13 21:17pm
   
If you want to prevent page refreshing, set autopostback as false. And do the selection of all checkboxes at client (via jquery).
anurag19289 11-Sep-13 23:28pm
   
so if i want to do it through server side...then this autopostback property has to be true ? is it correct ?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hii...
See this one,its may helpful to u.
In aspx
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('[id*=chkHeader]').click(function () {
                $("[id*='chkChild']").attr('checked', this.checked);
            });
        });
               </script>



<asp:GridView ID="gvUserInfo" runat="server" Width="561px" AutoGenerateColumns="False" >
        <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
        <Columns>
        <asp:TemplateField><HeaderTemplate>
        <asp:CheckBox ID="chkHeader" runat="server" />
        </HeaderTemplate>
        <ItemTemplate>
        <asp:CheckBox ID="chkChild"  runat="server"/>
        </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Id">
                <ItemTemplate>
                <asp:Label ID="lblId" runat="server" Text='<%# Eval("Id") %>'/>
                </ItemTemplate>
                </asp:TemplateField>
        <asp:TemplateField HeaderText="UserName">
                <ItemTemplate>
                <asp:Label ID="lblUserName" runat="server" Text='<%# Eval("UserName") %>'/>
                </ItemTemplate>
                </asp:TemplateField>
        <asp:TemplateField HeaderText="Email_Id">
                <ItemTemplate>
                <asp:Label ID="lblEmail_Id" runat="server" Text='<%# Eval("Email_Id") %>'/>
                </ItemTemplate>
                </asp:TemplateField> 
        </Columns>
        </asp:GridView>

Thank U.
   
Comments
anurag19289 12-Sep-13 5:29am
   
This will work...But is it possible to do without using javascript.
anurag19289 13-Sep-13 9:47am
   
Thanks bro :)
anurag19289 13-Sep-13 11:01am
   
This is working brilliantly :)
navin ks 7-Jan-14 0:24am
   
works
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

try this code........

add client side..

<div>
        <asp:Timer ID="Timer1" runat="server" Interval="1000">
        </asp:Timer>
    </div>

<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">

<Triggers>
 <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
 </Triggers>
 <ContentTemplate>
<asp:GridView ID="GridZeroApprovedKm" AllowPaging="true" runat="server" AutoGenerateColumns="false"

            DataKeyNames="Address_id" OnRowCancelingEdit="GridZeroApprovedKm_RowCancelingEdit"

            OnRowDataBound="GridZeroApprovedKm_RowDataBound" OnRowDeleting="GridZeroApprovedKm_RowDeleting"

            OnRowEditing="GridZeroApprovedKm_RowEditing" OnRowUpdating="GridZeroApprovedKm_RowUpdating">
            <SelectedRowStyle CssClass="ResultSelectedRow" BackColor="Cyan" />
            <columns>
<asp:TemplateField HeaderText="Select All">
 <HeaderTemplate>
<asp:CheckBox ID="chkb1" runat="server" Text="Select All" OnCheckedChanged="chkb1_CheckedChanged" AutoPostBack="true" />
</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemTemplate>
 <asp:CheckBox ID="chkb2" runat="server" Text="Select" />
 </ItemTemplate>
<ItemStyle HorizontalAlign="center" />
 </asp:TemplateField>
</ContentTemplate>
</asp:UpdatePanel>




add this code behind file....


protected void chkb1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox ChkBoxHeader = (CheckBox)gv_product.HeaderRow.FindControl("chkb1");
            foreach (GridViewRow row in gv_product.Rows)
            {
                CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkb2");
                if (ChkBoxHeader.Checked == true)
                {
                    ChkBoxRows.Checked = true;
                }
                else
                {
                    ChkBoxRows.Checked = false;
                }
            }
        }
   
v3
Comments
anurag19289 12-Sep-13 5:25am
   
Hi this is the same thing you have written what i have written above... but the page is refreshing.

i want to do this without using javascript...kindly help
Kunal_Patel 12-Sep-13 5:29am
   
ok.i update my above code..
Kunal_Patel 12-Sep-13 5:31am
   
i have change my code...try that it's working..
anurag19289 12-Sep-13 5:31am
   
let me try and get back to you...
Kunal_Patel 12-Sep-13 5:39am
   
ok..
anurag19289 12-Sep-13 5:39am
   
Hello KR-

It's working....But it seems its taking much time as compared to what javascript does....allow paging i have made to true in gridview. And 10 records are displayed per page.

and in code behind i have shortened the code as :

protected void sellectAll(object sender, EventArgs e)
{

foreach (GridViewRow row in GridZeroApprovedKm.Rows)
{
((CheckBox)row.FindControl("chkb2")).Checked = ((CheckBox)sender).Checked;
}
}


--so using the update panel and content template....why its taking much time ?
Kunal_Patel 12-Sep-13 5:54am
   
ok..bro

you have tell me without javascript so i provide this solution...

why i use updatepanel and content...??


visit this link http://www.aspdotnet-suresh.com/2012/05/ajax-updapanel-control-with-triggers.html
Kunal_Patel 12-Sep-13 6:03am
   
this is second way you can use(without javascript).......
Kunal_Patel 13-Sep-13 7:19am
   
Please up-vote, if you like it..
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

   
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190518.1 | Last Updated 13 Sep 2013
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100