clearing a Header Row CheckBox in a DataGrid

maqskywalker
maqskywalker used Ask the Experts™
on
Hi experts,

I'm using ASP.NET Web Forms with VB.

I have a DataGrid bound to a Sql Stored Procedure. The stored procedure fetches the Northwind Employees sql server table.

These are the SQL Scripts.

Script for the Table

USE [Northwind]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[NWEmployees](
	[EmployeeID] [int] NOT NULL,
	[LastName] [nvarchar](20) NOT NULL,
	[FirstName] [nvarchar](10) NOT NULL,
	[Title] [nvarchar](30) NULL,
	[TitleOfCourtesy] [nvarchar](25) NULL,
	[BirthDate] [datetime] NULL,
	[HireDate] [datetime] NULL,
	[Address] [nvarchar](60) NULL,
	[City] [nvarchar](15) NULL,
	[Region] [nvarchar](15) NULL,
	[PostalCode] [int] NULL,
	[Country] [nvarchar](15) NULL,
 CONSTRAINT [PK_Employees_NWEmployees] PRIMARY KEY CLUSTERED 
(
	[EmployeeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (1, N'Woods', N'Tiger', N'Actor', N'Ms.', CAST(N'1948-12-08 00:00:00.000' AS DateTime), CAST(N'1992-05-01 00:00:00.000' AS DateTime), N'507 - 20th Ave. E.
Apt. 2A', N'Seattle', N'WA', 93722, N'USA')
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (2, N'Fuller', N'Andrew', N'Vice President, Sales', N'Dr.', CAST(N'1952-02-19 00:00:00.000' AS DateTime), CAST(N'1992-08-14 00:00:00.000' AS DateTime), N'908 W. Capital Way', N'Tacoma', N'WA', 98401, N'USA')
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (3, N'Leverling', N'Janet', N'Sales Representative', N'Ms.', CAST(N'1963-08-30 00:00:00.000' AS DateTime), CAST(N'1992-04-01 00:00:00.000' AS DateTime), N'722 Moss Bay Blvd.', N'Kirkland', N'WA', 98033, N'USA')
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (4, N'Peacock', N'Margaret', N'Sales Representative', N'Mrs.', CAST(N'1937-09-19 00:00:00.000' AS DateTime), CAST(N'1993-05-03 00:00:00.000' AS DateTime), N'4110 Old Redmond Rd.', N'Redmond', N'WA', 98052, N'USA')
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (5, N'Buchanan', N'Steven', N'Sales Manager', N'Mr.', CAST(N'1955-03-04 00:00:00.000' AS DateTime), CAST(N'1993-10-17 00:00:00.000' AS DateTime), N'14 Garrett Hill', N'London', NULL, 95624, N'UK')
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (6, N'Suyama', N'Michael', N'Sales Representative', N'Mr.', CAST(N'1963-07-02 00:00:00.000' AS DateTime), CAST(N'1993-10-17 00:00:00.000' AS DateTime), N'Coventry House
Miner Rd.', N'London', NULL, 93722, N'UK')
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (7, N'King', N'Robert', N'Sales Representative', N'Mr.', CAST(N'1960-05-29 00:00:00.000' AS DateTime), CAST(N'1994-01-02 00:00:00.000' AS DateTime), N'Edgeham Hollow
Winchester Way', N'London', NULL, 45648, N'UK')
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (8, N'Callahan', N'Laura', N'Inside Sales Coordinator', N'Ms.', CAST(N'1958-01-09 00:00:00.000' AS DateTime), CAST(N'1994-03-05 00:00:00.000' AS DateTime), N'4726 - 11th Ave. N.E.', N'Seattle', N'WA', 98105, N'USA')
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (9, N'Dodsworth', N'Anne', N'Sales Representative', N'Ms.', CAST(N'1966-01-27 00:00:00.000' AS DateTime), CAST(N'1994-11-15 00:00:00.000' AS DateTime), N'7 Houndstooth Rd.', N'London', NULL, 98751, N'UK')
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (10, N'Duck', N'Daffy', N'Sailor', NULL, NULL, NULL, NULL, NULL, NULL, 90210, NULL)
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (11, N'Lavigne', N'Avril', N'Rock Star', NULL, NULL, NULL, NULL, NULL, NULL, 90210, NULL)
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (12, N'Woods', N'Tiger', N'Golfer', NULL, NULL, NULL, NULL, NULL, NULL, 90210, NULL)
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (13, N'Smith', N'Laura', N'Inside Sales Coordinator', N'Ms.', CAST(N'1958-01-09 00:00:00.000' AS DateTime), CAST(N'1994-03-05 00:00:00.000' AS DateTime), N'4726 - 11th Ave. N.E.', N'Seattle', N'WA', 98105, N'USA')
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (14, N'Johnson', N'Laura', N'Inside Sales Coordinator', N'Ms.', CAST(N'1958-01-09 00:00:00.000' AS DateTime), CAST(N'1994-03-05 00:00:00.000' AS DateTime), N'4726 - 11th Ave. N.E.', N'Seattle', N'WA', 98105, N'USA')
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (15, N'Camaney', N'Laura', N'Inside Sales Coordinator', N'Ms.', CAST(N'1958-01-09 00:00:00.000' AS DateTime), CAST(N'1994-03-05 00:00:00.000' AS DateTime), N'4726 - 11th Ave. N.E.', N'Seattle', N'WA', 98105, N'USA')
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (16, N'Wilson', N'Laura', N'Inside Sales Coordinator', N'Ms.', CAST(N'1958-01-09 00:00:00.000' AS DateTime), CAST(N'1994-03-05 00:00:00.000' AS DateTime), N'4726 - 11th Ave. N.E.', N'Seattle', N'WA', 98105, N'USA')
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (17, N'Linney', N'Laura', N'Inside Sales Coordinator', N'Ms.', CAST(N'1958-01-09 00:00:00.000' AS DateTime), CAST(N'1994-03-05 00:00:00.000' AS DateTime), N'4726 - 11th Ave. N.E.', N'Seattle', N'WA', 98105, N'USA')
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (18, N'Roberts', N'Laura', N'Inside Sales Coordinator', N'Ms.', CAST(N'1958-01-09 00:00:00.000' AS DateTime), CAST(N'1994-03-05 00:00:00.000' AS DateTime), N'4726 - 11th Ave. N.E.', N'Seattle', N'WA', 98105, N'USA')
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (19, N'Dunn', N'Laura', N'Inside Sales Coordinator', N'Ms.', CAST(N'1958-01-09 00:00:00.000' AS DateTime), CAST(N'1994-03-05 00:00:00.000' AS DateTime), N'4726 - 11th Ave. N.E.', N'Seattle', N'WA', 98105, N'USA')
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (20, N'Taylor', N'Laura', N'Inside Sales Coordinator', N'Ms.', CAST(N'1958-01-09 00:00:00.000' AS DateTime), CAST(N'1994-03-05 00:00:00.000' AS DateTime), N'4726 - 11th Ave. N.E.', N'Seattle', N'WA', 98105, N'USA')
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (21, N'Crawford', N'Laura', N'Inside Sales Coordinator', N'Ms.', CAST(N'1958-01-09 00:00:00.000' AS DateTime), CAST(N'1994-03-05 00:00:00.000' AS DateTime), N'4726 - 11th Ave. N.E.', N'Seattle', N'WA', 98105, N'USA')
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (22, N'Fawcett', N'Laura', N'Inside Sales Coordinator', N'Ms.', CAST(N'1958-01-09 00:00:00.000' AS DateTime), CAST(N'1994-03-05 00:00:00.000' AS DateTime), N'4726 - 11th Ave. N.E.', N'Seattle', N'WA', 98105, N'USA')
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (23, N'Ramirez', N'Laura', N'Inside Sales Coordinator', N'Ms.', CAST(N'1958-01-09 00:00:00.000' AS DateTime), CAST(N'1994-03-05 00:00:00.000' AS DateTime), N'4726 - 11th Ave. N.E.', N'Seattle', N'WA', 98105, N'USA')
INSERT [dbo].[NWEmployees] ([EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City], [Region], [PostalCode], [Country]) VALUES (25, N'Reyna', N'Laura', N'Inside Sales Coordinator', N'Ms.', CAST(N'1958-01-09 00:00:00.000' AS DateTime), CAST(N'1994-03-05 00:00:00.000' AS DateTime), N'4726 - 11th Ave. N.E.', N'Seattle', N'WA', 98105, N'USA')

Open in new window


Script for the stored procedure:

USE [Northwind]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[NWEmployees_Get]
AS

SELECT [EmployeeID]
      ,[LastName]
      ,[FirstName]
      ,[Title]
      ,[BirthDate]
      ,[HireDate]
      ,[Address]
      ,[City]
      ,[Region]
      ,[PostalCode]
      ,[Country]
FROM [Northwind].[dbo].[NWEmployees]


GO

Open in new window


This is the Code for DGEmployees.aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="DGEmployees.aspx.vb" Inherits="DataGridCheckBoxes.DGEmployees" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .DGHeaderText {
            font-family: Arial;
            font-size: 12px;
            color: #000000;
            font-weight: bold;
        }

        .DGBodyText {
            font-family: Arial;
            font-size: 12px;
            color: #000000;
            font-weight: normal;
        }
    </style>
    <script type="text/javascript">
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>Reset Header CheckBox</td>
                    <td><asp:Button ID="Button1" runat="server" Text="Reset Header TB" /></td>                    
                </tr>
                <tr>
                    <td>Reset Body Grid Row CheckBoxes</td>
                    <td><asp:Button ID="Button2" runat="server" Text="Reset Body TB" /></td>                    
                </tr>
            </table>
        </div>
        <div id="DivEmployees">
            <asp:DataGrid ID="DGEmployees" runat="server" AutoGenerateColumns="False" Font-Size="8pt" CellPadding="3" BorderColor="Black" Width="400px" CssClass="DGHeaderText">
                <Columns>
                    <asp:TemplateColumn HeaderText="contract">
                        <HeaderTemplate>
                            <asp:CheckBox runat="server" ID="DataGridEmployees_SelectAll" autopostback="true" OnCheckedChanged="DataGridEmployees_SelectAll_Click" />
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="EmpId" name="EmpId" runat="server" AutoPostBack="true" OnCheckedChanged="ChckedChanged" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="EmployeeID">
                        <ItemTemplate>
                            <asp:Label ID="EmployeeID" CssClass="DGBodyText" Text='<%# DataBinder.Eval(Container.DataItem, "EmployeeID") %>' runat="server" />
                        </ItemTemplate>
                        <ItemStyle HorizontalAlign="Center" />
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="FirstName">
                        <ItemTemplate>
                            <asp:Label ID="FirstName" CssClass="DGBodyText" Text='<%# DataBinder.Eval(Container.DataItem, "FirstName") %>' runat="server" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="LastName">
                        <ItemTemplate>
                            <asp:Label ID="LastName" CssClass="DGBodyText" Text='<%# DataBinder.Eval(Container.DataItem, "LastName") %>' runat="server" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="Title">
                        <ItemTemplate>
                            <asp:Label ID="Title" CssClass="DGBodyText" Text='<%# DataBinder.Eval(Container.DataItem, "Title") %>' runat="server" />
                        </ItemTemplate>
                        <ItemStyle Wrap="False" />
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="PostalCode">
                        <ItemTemplate>
                            <asp:Label ID="PostalCode" CssClass="DGBodyText" Text='<%# DataBinder.Eval(Container.DataItem, "PostalCode") %>' runat="server" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                </Columns>
                <HeaderStyle HorizontalAlign="Center" />
            </asp:DataGrid>
            <br />
        </div>
        <br />
        <div id="DivTestLabel2">  
            <asp:Label ID="TestLabel2" runat="server" />
        </div>
        <br />
        <asp:HiddenField ID="HiddenFieldTest1" runat="server" />
        <%-- this hidden field holds the selected employees from the employees DataGrid --%>
        <asp:HiddenField ID="hfSelectedEmployees" runat="server" />       
    </form>
</body>
</html>

Open in new window


This is the code for DGEmployees.aspx.vb

Imports System.Data
Imports System.Data.SqlClient

Public Class DGEmployees
    Inherits System.Web.UI.Page

    ' ----- Page Load Event -----
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            BindDGEmployees()
        End If

    End Sub
    ' ----- Page Load Event -----
    ' ----- DataGrid Bind Function -----
    Private Sub BindDGEmployees()

        Dim constr As String = ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString

        Using mySqlConnection As SqlConnection = New SqlConnection(constr)

            mySqlConnection.Open()

            Dim mySqlCommand As SqlCommand = mySqlConnection.CreateCommand()
            ' sproc name
            mySqlCommand.CommandText = "NWEmployees_Get"
            mySqlCommand.CommandType = CommandType.StoredProcedure
            Dim mySqlDataAdapter As SqlDataAdapter = New SqlDataAdapter()
            mySqlDataAdapter.SelectCommand = mySqlCommand
            Dim ds As DataSet = New DataSet()
            mySqlDataAdapter.Fill(ds, "NWEmployees")
            DGEmployees.DataSource = ds.Tables("NWEmployees").DefaultView
            DGEmployees.DataBind()

            mySqlConnection.Close()

        End Using

    End Sub
    ' ----- DataGrid Bind Function -----
    ' ----- asp check box click event - good -----
    Protected Sub ChckedChanged(ByVal sender As Object, ByVal e As EventArgs)

        ' ********** Create String List from Checked Checkboxes and store in hidden field ********** 
        ' create list
        Dim list As List(Of String) = New List(Of String)

        ' ----- loop through checked DataGrid items -----
        For Each di As DataGridItem In DGEmployees.Items
            Dim chkBx As CheckBox = CType(di.FindControl("EmpId"), CheckBox)

            If chkBx IsNot Nothing AndAlso chkBx.Checked Then
                Dim lbl As Label = CType(di.FindControl("EmployeeID"), Label)
                list.Add(lbl.Text)
            End If
        Next
        ' -----------------------------------------------

        Dim strSelectedEmployeesList As String = String.Join(",", list)
        'TestLabel2.Text = "Hello Label 2"
        TestLabel2.Text = strSelectedEmployeesList
        ' Response.Write(TestString1 & "<br>")
        ' save Employees selected string value in hidden field
        hfSelectedEmployees.Value = strSelectedEmployeesList
        ' ********** Create String List from Checked Checkboxes and store in hidden field **********         

    End Sub
    ' ----- asp check box click event - good -----

    ' ----- ASP CheckBox - Header Row Checkbox Changed Click Event - good -----
    Public Sub DataGridEmployees_SelectAll_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Call CheckAllBoxesOnGrid(sender, Me.DGEmployees)

        ' ********** Create String List from Checked Checkboxes and store in hidden field ********** 
        ' create list
        Dim list As List(Of String) = New List(Of String)

        ' ----- loop through checked DataGrid items -----
        For Each di As DataGridItem In DGEmployees.Items
            Dim chkBx As CheckBox = CType(di.FindControl("EmpId"), CheckBox)

            If chkBx IsNot Nothing AndAlso chkBx.Checked Then
                Dim lbl As Label = CType(di.FindControl("EmployeeID"), Label)
                list.Add(lbl.Text)
            End If
        Next
        ' -----------------------------------------------

        ' separate each list item by a comma and save in a string variable
        Dim strSelectedEmployeesList As String = String.Join(",", list)
        ' display string list variable in label calles TestLabel2 on button click
        TestLabel2.Text = strSelectedEmployeesList

        ' save string list variable value in hidden field called hfSelectedEmployees
        hfSelectedEmployees.Value = strSelectedEmployeesList
        ' ********** Create String List from Checked Checkboxes and store in hidden field **********      
    End Sub
    ' ----- ASP CheckBox - Header Row Checkbox Changed Click Event - good -----

    ' ----- Check All CheckBoxes on DataGrid Function -----
    Private Sub CheckAllBoxesOnGrid(ByVal sender As Object, ByRef DG As Web.UI.WebControls.DataGrid)
        For Each DGI As DataGridItem In DG.Items
            Dim chkSelect As Object
            chkSelect = DGI.FindControl("EmpId")
            If (chkSelect.Enabled = "True") Then
                chkSelect.Checked = sender.Checked

            End If
        Next
    End Sub
    ' ----- Check All CheckBoxes on DataGrid Function -----

    ' ----- Button Click - Reset Header CheckBox of DataGrid -----
    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        ' reset header checkbox
        Call ResetHeaderCheckBox(sender, Me.DGEmployees)

    End Sub
    ' ----- Button Click - Reset Header CheckBox of DataGrid -----


    ' ----- Button Click - Reset All CheckBoxes on Body of DataGrid -----
    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        ' reset all body checkboxes
        Call ResetCheckBoxesOnBodyOfGrid(sender, Me.DGEmployees)

    End Sub

    ' ----- Button Click - Reset All CheckBoxes on Body of DataGrid -----
    ' ----- Reset Header CheckBox of DataGrid -----
    Private Sub ResetHeaderCheckBox(ByVal sender As Object, ByRef DG As Web.UI.WebControls.DataGrid)

        Dim Check As New CheckBox
        Check = CType(Me.DGEmployees.FindControl("DataGridEmployees_SelectAll"), CheckBox)
        Check.Checked = False

    End Sub
    ' ----- Reset Header CheckBox of DataGrid -----
    ' ----- Reset All CheckBoxes on Body of DataGrid -----
    Private Sub ResetCheckBoxesOnBodyOfGrid(ByVal sender As Object, ByRef DG As Web.UI.WebControls.DataGrid)
        For Each DGI As DataGridItem In DG.Items
            Dim chkSelect As Object
            chkSelect = DGI.FindControl("EmpId")
            chkSelect.Checked = False
        Next
    End Sub
    ' ----- Reset All CheckBoxes on Body of DataGrid -----


End Class

Open in new window



When I run my page it looks like this:

Run1.PNG
So if I click in the Grid's header textbox that places a check in every checkbox in the grid like this:

Run2.PNG
Now if I click on the button that says Reset Body Tb, that clears all the checks in the checkboxes in the rows of the DataGrid.

Notice after I press that button the only checkbox that is not cleared is the header row checkbox.

Run3.PNG

Now notice there is another button that says Reset Header TB.  When I press this button I want to clear the Header Row checkbox.
But when I press the button I get this error:

Run5.PNG
The button click event for the Reset Header TB button looks like this:

    ' ----- Button Click - Reset Header CheckBox of DataGrid -----
    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        ' reset header checkbox
        Call ResetHeaderCheckBox(sender, Me.DGEmployees)

    End Sub
    ' ----- Button Click - Reset Header CheckBox of DataGrid -----

So then the reset header function looks like this:

    ' ----- Reset Header CheckBox of DataGrid -----
    Private Sub ResetHeaderCheckBox(ByVal sender As Object, ByRef DG As Web.UI.WebControls.DataGrid)

        Dim Check As New CheckBox
        Check = CType(Me.DGEmployees.FindControl("DataGridEmployees_SelectAll"), CheckBox)
        Check.Checked = False

    End Sub
    ' ----- Reset Header CheckBox of DataGrid -----

What am I doing wrong?
When I press  Button1 if there is a check in the header row checkbox I want to clear it.

Here is a demo video:

https://youtu.be/9S_ai1gA-M0
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Please try

 CType(DGEmployees.Items(0).Cells(0).FindControl("DataGridEmployees_SelectAll"), CheckBox).Checked = False

Open in new window

Author

Commented:
I put it inside the button click like this but still get this error

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        CType(DGEmployees.Items(0).Cells(0).FindControl("DataGridEmployees_SelectAll"), CheckBox).Checked = False

    End Sub

e2.PNG
Chief Technology Ninja
Distinguished Expert 2018
Commented:
Hi Mike,

I forgot about the controls.controls as we have to access the inner control hence the entire error.
Please use this code to access your header checkbox

Dim dgHeader As DataGridItem = DGEmployees.Controls(0).Controls(0)
        Dim targetChkBox As CheckBox = dgHeader.Controls(0).Controls(1)
        targetChkBox.Checked = False

Open in new window


I am sure you can use FindControl method as shown above but then you will have to try something else
1. Set ClientIDMode to Static on the Header Checkbox
<asp:CheckBox runat="server" ID="DataGridEmployees_SelectAll" autopostback="true" OnCheckedChanged="DataGridEmployees_SelectAll_Click"  ClientIDMode="Static" />

Open in new window

We need to set it to StaticID so ASP.Net runtime does not change it(prefixing ctl00_ and so on)
2. Use this code
CType(DGEmployees.Controls(0).Controls(0).FindControl("DataGridEmployees_SelectAll"), CheckBox).Checked = False

Open in new window


Regards,
Chinmay.

Author

Commented:
Thanks. Worked great!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial