You must Sign In to post a response.
  • Category: ASP.NET

    Check and uncheck all checkbox from linkbutton in gridview

    Hi All,
    look at my code ,I want to do the functionally like when I will click on "Check All" Link button then my all check box should be select and when I click "unCheck All" linkbutton theb all check should be unselect.
    this is my code
    function SelectAll(chk) {
    //get reference of GridView control
    var grid = document.getElementById("<%= CourseGridView.ClientID %>");
    //variable to contain the cell of the grid
    var cell;

    if (grid.rows.length > 0) {
    //loop starts from 1. rows[0] points to the header.
    for (i = 1; i < grid.rows.length; i++) {
    //get the reference of first column
    cell = grid.rows[i].cells[0];

    //loop according to the number of childNodes in the cell
    for (j = 0; j < cell.childNodes.length; j++) {
    //if childNode type is CheckBox
    if (cell.childNodes[j].type == "checkbox" && cell.childNodes[j].id.indexOf('chkSel') != -1) {
    //assign the status of the Select All checkbox to the cell checkbox within the grid
    cell.childNodes[j].checked = chk;
    }
    }
    }
    }
    }

    <asp:LinkButton ID="lnkCheck" Text="Check all" OnClientClick=" SelectAll(true); return false;"
    runat="server"></asp:LinkButton><br />
    <asp:LinkButton ID="lnkUncheck" Text="Uncheck all" OnClientClick=" SelectAll(false); return false;"
    runat="server"></asp:LinkButton><br />
    <asp:GridView ID="CourseGridView" runat="server" AutoGenerateColumns="false" AutoGenerateSelectButton="True"
    CssClass="gridview" OnSelectedIndexChanged="CourseGridView_SelectedIndexChanged"
    OnDataBound="CourseGridView_DataBound" OnRowDataBound="CourseGridView_RowDataBound">
    <Columns>
    <asp:TemplateField>
    <HeaderTemplate>
    Select All
    <asp:CheckBox ID="chkAll" runat="server" onclick="SelectAll(this.checked);" />
    <%-- <asp:LinkButton ID="checklink" runat="server" ForeColor="White" CausesValidation="false" Text="Select All" ></asp:LinkButton>
    <asp:LinkButton ID="unchecklink" runat="server" ForeColor="White">Clear All</asp:LinkButton>--%>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:CheckBox ID="chkSel" runat="server" />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Course ID">
    <ItemTemplate>
    <%# Eval("Course_ID") %>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Course Name">
    <ItemTemplate>
    <%# Eval("Course_Name")%>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Title">
    <ItemTemplate>
    <%# Eval("Title")%>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Description">
    <ItemTemplate>
    <%# Eval("Description")%>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Course Type">
    <ItemTemplate>
    <%# Eval("Course_Type") %>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    <PagerStyle CssClass="gridViewPager" />
    <SelectedRowStyle CssClass="gridViewSelectedRow" />
    <HeaderStyle CssClass="gridViewHeader" />
    <AlternatingRowStyle CssClass="gridViewAltRow" />
    </asp:GridView>

    but actually it is not working ,
  • #582079
    Hi Asha,

    I posted an article for select and unselect the gridview checkboxes while clicking the select all check box. Please refer my article,
    http://www.dotnetspider.com/resources/37219-Check-all-options-using-javascript.aspx

    I hope to useful this.

    Regards,
    V.M. Damodharan
    "Your talent will be worthless, when you have fear and tension."

  • #582089
    try this code for check all

    foreach (GridViewRow Gvr in Grid1.Rows)
    {
    var Cb = (CheckBox)Convert.ChangeType((Gvr.FindControl("chkSel")), typeof(CheckBox));
    Cb.Checked = true;
    }


    and for uncheckall try this code



    foreach (GridViewRow Gvr in Grid1.Rows)
    {
    var Cb = (CheckBox)Convert.ChangeType((Gvr.FindControl("chkSel")), typeof(CheckBox));
    Cb.Checked = false;
    }



    hope this will help you

  • #582263
    Please check the following code:


    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title>Untitled Page</title>

    <script type="text/javascript">
    function SelectAll(chk) {
    var gridViewCtlId = '<%=CourseGridView.ClientID%>';
    var grid = document.getElementById(gridViewCtlId);
    var gridLength = grid.rows.length;
    for (var i = 1; i < gridLength; i++) {
    cell = grid.rows[i].cells[1];
    for (var j = 0; j < cell.childNodes.length; j++) {
    if (cell.childNodes[j].type == 'checkbox') {
    cell.childNodes[j].checked = chk;
    }
    }
    }
    }


    </script>

    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:LinkButton ID="lnkCheck" Text="Check all" OnClientClick=" SelectAll(true); return false;"
    runat="server"></asp:LinkButton><br />
    <asp:LinkButton ID="lnkUncheck" Text="Uncheck all" OnClientClick=" SelectAll(false); return false;"
    runat="server"></asp:LinkButton><br />
    <asp:GridView ID="CourseGridView" runat="server" AutoGenerateColumns="false" AutoGenerateSelectButton="True"
    CssClass="gridview" OnSelectedIndexChanged="CourseGridView_SelectedIndexChanged"
    OnDataBound="CourseGridView_DataBound" OnRowDataBound="CourseGridView_RowDataBound">
    <Columns>
    <asp:TemplateField>
    <HeaderTemplate>
    Select All
    <asp:CheckBox ID="chkAll" runat="server" onclick="SelectAll(this.checked);" />
    <%-- <asp:LinkButton ID="checklink" runat="server" ForeColor="White" CausesValidation="false" Text="Select All" ></asp:LinkButton>
    <asp:LinkButton ID="unchecklink" runat="server" ForeColor="White">Clear All</asp:LinkButton>--%>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:CheckBox ID="chkSel" runat="server" />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Course ID">
    <ItemTemplate>
    <%# Eval("Course_ID") %>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Course Name">
    <ItemTemplate>
    <%# Eval("Course_Name")%>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Title">
    <ItemTemplate>
    <%# Eval("Title")%>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Description">
    <ItemTemplate>
    <%# Eval("Description")%>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Course Type">
    <ItemTemplate>
    <%# Eval("Course_Type") %>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    <PagerStyle CssClass="gridViewPager" />
    <SelectedRowStyle CssClass="gridViewSelectedRow" />
    <HeaderStyle CssClass="gridViewHeader" />
    <AlternatingRowStyle CssClass="gridViewAltRow" />
    </asp:GridView>
    </div>
    </form>
    </body>
    </html>


    Thanks & Regards
    Paritosh Mohapatra
    Microsoft MVP (ASP.Net/IIS)
    DotNetSpider MVM


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.