Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

Gridview Select All rows with Checkbox

$
0
0

Hi,

I have a gridview with rows and a select all checkbox in the header, I am using javascript to select all checkboxes but its not working.

blow is th code.

ASPX PAGE

<%@ Page Title="Applicant Pool" Language="C#" MasterPageFile="~/MasterPage/Company.Master" AutoEventWireup="true" CodeBehind="ApplicantPool.aspx.cs" Inherits="OnlineApplication.Company.Applications.ApplicantPool" %><asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"><script type="text/javascript">
        function SelectheaderCheckboxes(headerchk) {
            debugger
            var gvcheck = document.getElementById(<%=gvApplicants.ClientID%>);
            var i;
            //Condition to check header checkbox selected or not if that is true checked all checkboxes
            if (headerchk.checked) {
                for (i = 0; i < gvcheck.rows.length - 1; i++) {
                    var inputs = gvcheck.rows[i].getElementsByTagName('input');
                    inputs[0].checked = true;
                }
            }
                //if condition fails uncheck all checkboxes in gridview
            else {
                for (i = 0; i < gvcheck.rows.length - 1; i++) {
                    var inputs = gvcheck.rows[i].getElementsByTagName('input');
                    inputs[0].checked = false;
                }
            }
        }</script></asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"><asp:UpdatePanel ID="upnlApplicantPool" runat="server"><Triggers></Triggers><ContentTemplate><table><tr><td colspan="3">Search Options</td></tr><tr><td>Keywords</td><td><asp:TextBox ID="txtKeyword" runat="server"></asp:TextBox></td><td rowspan="5"><asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" /></td></tr><tr><td>Work experience </td><td><asp:DropDownList ID="ddlWorkExperience" runat="server"><asp:ListItem Selected="True" Value="warehouseoperative">Warehouse Operative/Order Picking</asp:ListItem><asp:ListItem Value="machineoperating">Machine Operating</asp:ListItem><asp:ListItem Value="labourer">Labourer</asp:ListItem><asp:ListItem Value="assemblyexperience">Assembly</asp:ListItem><asp:ListItem Value="foodproduction">Food Production</asp:ListItem><asp:ListItem Value="qcinspection">Quality Control/Inspection</asp:ListItem><asp:ListItem Value="packing">Packing</asp:ListItem><asp:ListItem Value="forklifttruckdriver">Forklift Driving</asp:ListItem></asp:DropDownList></td></tr><tr><td>Perefence</td><td><asp:DropDownList ID="ddlPreference" runat="server"><asp:ListItem Selected="True" Value="warehouseoperative">Warehouse Operative/Order Picking</asp:ListItem><asp:ListItem Value="machineoperating">Machine Operating</asp:ListItem><asp:ListItem Value="labourer">Labourer</asp:ListItem><asp:ListItem Value="assemblypreference">Assembly</asp:ListItem><asp:ListItem Value="foodproduction">Food Production</asp:ListItem><asp:ListItem Value="qcinspection">Quality Control/Inspection</asp:ListItem><asp:ListItem Value="packing">Packing</asp:ListItem><asp:ListItem Value="fortlifttruckdriver">Forklift Driving</asp:ListItem></asp:DropDownList></td></tr><tr><td>Availability</td><td><asp:DropDownList ID="ddlAvailability" runat="server"><asp:ListItem Value="morning">Morning</asp:ListItem><asp:ListItem>Afternoon</asp:ListItem><asp:ListItem Value="sixtwotwoonezero">6-2 / 2-10</asp:ListItem><asp:ListItem Value="days">Days</asp:ListItem><asp:ListItem Value="nights">Nights</asp:ListItem><asp:ListItem Value="schoolhoursonly">School hours only</asp:ListItem><asp:ListItem Value="weekends">Weekends</asp:ListItem><asp:ListItem Value="bankholidays">Bank Holidays</asp:ListItem></asp:DropDownList></td></tr><tr><td>Ditance </td><td><asp:DropDownList ID="ddlDistance" runat="server" AppendDataBoundItems="true"><asp:ListItem></asp:ListItem><asp:ListItem Value="1">1 mile</asp:ListItem><asp:ListItem Value="2">2 miles</asp:ListItem><asp:ListItem Value="3">3 miles</asp:ListItem><asp:ListItem Value="4">4 miles</asp:ListItem><asp:ListItem Value="5">5 miles</asp:ListItem><asp:ListItem Value="6">6 miles</asp:ListItem><asp:ListItem Value="7">7 miles</asp:ListItem><asp:ListItem Value="8">8 miles</asp:ListItem><asp:ListItem Value="9">9 miles</asp:ListItem><asp:ListItem Value="10">10 miles</asp:ListItem><asp:ListItem Value="15">15 miles</asp:ListItem><asp:ListItem Value="20">20 miles</asp:ListItem><asp:ListItem Value="25">25 miles</asp:ListItem><asp:ListItem Value="30">30 miles</asp:ListItem><asp:ListItem Value="35">35 miles</asp:ListItem><asp:ListItem Value="40">40 miles</asp:ListItem><asp:ListItem Value="45">45 miles</asp:ListItem><asp:ListItem Selected="True" Value="50">50 miles</asp:ListItem></asp:DropDownList></td></tr></table><table><tr><td>Search Result (a list of applicant that have registered and are looking for work) </td></tr><tr><td><asp:HiddenField ID="hfSelectedApplicants" runat="server" /><asp:GridView OnRowDataBound="gvApplicants_RowDataBound" ID="gvApplicants" runat="server" DataKeyNames="ApplicantId" AutoGenerateColumns="false" ShowHeaderWhenEmpty="true" AllowPaging="true" PageSize="10"><Columns><asp:TemplateField HeaderText="Select"><ItemTemplate><asp:CheckBox ID="chkChild" runat="server" /></ItemTemplate><HeaderTemplate><asp:CheckBox ID="chkHeader" Text="Select All" runat="server" onclick="javascript:SelectheaderCheckboxes(this)" /></HeaderTemplate></asp:TemplateField><asp:TemplateField HeaderText="Status"><ItemTemplate><asp:Label ID="lblStatus" runat="server" Text='<%#Bind("Status") %>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Name"><ItemTemplate><asp:Label ID="lblName" runat="server" Text='<%#Bind("Name") %>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="DOB"><ItemTemplate><asp:Label ID="lblBirthDate" runat="server" Text='<%#Bind("BirthDate") %>' ToolTip='<%#Bind("Age") %>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Home Phone"><ItemTemplate><asp:Label ID="lblHomePhone" runat="server" Text='<%#Bind("HomePhone") %>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Mobile"><ItemTemplate><asp:Label ID="lblMobile" runat="server" Text='<%#Bind("Mobile") %>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="City"><ItemTemplate><asp:Label ID="lblCity" runat="server" Text='<%#Bind("City") %>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Postcode"><ItemTemplate><asp:Label ID="lblPostCode" runat="server" Text='<%#Bind("Postcode") %>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Distance"><ItemTemplate><asp:Label ID="lblDistance" runat="server" Text='<%#Bind("Distance") %>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Work Experience"><ItemTemplate><asp:Label ID="lblWorkExperience" runat="server" Text='<%#Bind("WorkExpereience") %>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Preference"><ItemTemplate><asp:Label ID="lblPreference" runat="server" Text='<%#Bind("Preference") %>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Availability"><ItemTemplate><asp:Label ID="lblAvaialability" runat="server" Text='<%#Bind("Availability") %>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Distance"><ItemTemplate><asp:Label ID="lblNationality" runat="server" Text='<%#Bind("Nationality") %>'></asp:Label></ItemTemplate></asp:TemplateField></Columns></asp:GridView></td></tr><tr><td><asp:Button ID="btnAssign" runat="server" Text="Assign Selected Applicants to my site" /></td></tr><tr><td><br /><br /></td></tr><tr><td>Applicants assigned to my site (a list of applicants that have applied for a job or site has added them)</td></tr><tr><td><asp:GridView ID="gvAssigned" runat="server" DataKeyNames="ApplicantId,AdvertId" AutoGenerateColumns="false" ShowHeaderWhenEmpty="true" AllowPaging="true" PageSize="10"></asp:GridView></td></tr></table></ContentTemplate></asp:UpdatePanel><asp:UpdateProgress ID="uprgApplicantPool" AssociatedUpdatePanelID="upnlApplicantPool" runat="server"><ProgressTemplate><div style="background-color: Gray; filter: alpha(opacity=60); opacity: 0.60; width: 100%; top: 0px; left: 0px; position: fixed; height: 100%;"></div><div style="margin: auto; font-family: Trebuchet MS; filter: alpha(opacity=100); opacity: 1; font-size: small; vertical-align: middle; top: 45%; position: fixed; right: 45%; color: #275721; text-align: center; background-color: White; height: 100px;"><table style="background-color: White; font-family: Sans-Serif; text-align: center; border: solid 1px #275721; color: #275721; width: inherit; height: inherit; padding: 15px;"><tr><td style="text-align: inherit;"><img src="../../App_Image/Saving.gif" alt="Doing stuff behind the scene" /></td><td style="text-align: inherit;"><span style="font-family: Sans-Serif; font-size: medium; font-weight: bold;">Please wait...</span></td></tr></table></div></ProgressTemplate></asp:UpdateProgress></asp:Content>



C# PAGE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineApplication.App_Code;

namespace OnlineApplication.Company.Applications
{
    public partial class ApplicantPool : System.Web.UI.Page
    {
        App_Code.ApplicantPool ap = new App_Code.ApplicantPool();
        StafflineUser su = new StafflineUser();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindSearchResult();
        }

        protected void BindSearchResult()
        {
            ap.WorkExperience = ddlWorkExperience.SelectedItem.Value.ToString();
            ap.Preference = ddlPreference.SelectedItem.Value.ToString();
            ap.Availability = ddlAvailability.SelectedItem.Value.ToString();
            ap.Distance = Convert.ToInt16(ddlDistance.SelectedItem.Value.ToString());
            ap.ProjectCode = "1SISU";

            gvApplicants.DataSource = ap.SearchApplicantPool(ap);
            gvApplicants.DataBind();

        }

        protected void gvApplicants_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    CheckBox chkAll = (CheckBox)gvApplicants.HeaderRow.FindControl("cbSelectAll");
            //    CheckBox chkItem = (CheckBox)e.Row.FindControl("cbSelect");

            //    chkItem.Attributes.Add("onclick", "Selectchildcheckboxes('" + chkAll.ClientID + "')");
            //}
        }
    }
}



Error Message

0x800a138f - JavaScript runtime error: Unable to get property 'rows' of undefined or null reference

please help.

Thanks,

Maulin.


Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>