Click here to Skip to main content
15,898,010 members
Please Sign up or sign in to vote.
3.55/5 (4 votes)
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 ?

Hii...
See this one,its may helpful to u.
In aspx
XML
<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>



XML
<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.
 
Share this answer
 
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
try this code........

add client side..

XML
<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;
                }
            }
        }
 
Share this answer
 
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
[no name] 12-Sep-13 5:29am    
ok.i update my above code..
[no name] 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...
[no name] 12-Sep-13 5:39am    
ok..
 
Share this answer
 
v2

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