Checking all CheckBoxes in a GridView

Scott Mitchell has written another article with simple and clean implementation of providing check/uncheck all functionality in GridView.

Checking All CheckBoxes in a GridView

Only thing his implementation missing in the article is placing CheckBox in header of the GridView, and using it to toggle rows CheckBoxes. I have made small modification in the existing code to provide this feature:

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

    If Not Page.IsPostBack Then
        Dim dirInfo As New DirectoryInfo(Request.PhysicalApplicationPath)

        FileList.DataSource = dirInfo.GetFiles()
    End If

    'On every page visit we need to build up the CheckBoxIDs array
    For Each gvr As GridViewRow In FileList.Rows
        'Get a programmatic reference to the CheckBox control
        Dim cb As CheckBox = CType(gvr.FindControl("RowLevelCheckBox"), CheckBox)

        ClientScript.RegisterArrayDeclaration("CheckBoxIDs", String.Concat("'", cb.ClientID, "'"))

    '--modified code
    Dim checkAll As New CheckBox()
    checkAll.Text = ""
    checkAll.ID = "checkAll"
    checkAll.Attributes.Add("onclick", "ChangeAllCheckBoxStates(this.checked);")

End Sub