Asp.Net Checkbox to Check All or Clear All In Header Column Doesn't Keep State

1k views Asked by At

I have a Checkbox Control (column) in a header template field, for a Gridview control. On page load the gridview reads data from the server for each row. What I want is, when the header Checkbox control is checked, it updates the each row to either true or false (checked or unchecked) based on the checked state of the header column. The thing is after its checked, the header Checkbox it doesn't keep the checked state, so if I checked it, after posting the data back to the server it defaults to unchecked, I want it to keep it state, so I can uncheck it and save back to the database.

Here is the Code from the my .ASPX page

<asp:GridView ID="CategoriesGridView" runat="server" AutoGenerateColumns="false" EmptyDataText="No files Uploaded" BorderWidth="1px" BackColor="White"
AllowPaging="False" CellPadding="3" BorderStyle="None" BorderColor="#CCCCCC" Font-Names="Arial"
DataKeyNames="ID">
<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
<PagerStyle ForeColor="#000066" HorizontalAlign="Left"
    BackColor="White"></PagerStyle>
<HeaderStyle ForeColor="White" Font-Bold="True"
    BackColor="#006699"></HeaderStyle>
<Columns>
    <asp:TemplateField>                        
            <HeaderTemplate>
                <asp:CheckBox ID="chkAll" runat="server" Text="Show Discount" ItemStyle-Width="110" AutoPostBack="True" OnCheckedChanged="chkAll_CheckedChanged"/>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="chkShowDiscountImage" runat="server" Checked='<%# Eval("ShowDiscountImage")%>'></asp:CheckBox>
            </ItemTemplate>

    </asp:TemplateField>      
    <asp:CommandField ButtonType="Link" ShowEditButton="true" />
</Columns>
<SelectedRowStyle ForeColor="White" Font-Bold="True"
    BackColor="#669999"></SelectedRowStyle>
<RowStyle ForeColor="#000066"></RowStyle>

And here is the code form the code behind for the checkbox

Protected Sub chkAll_CheckedChanged(sender As Object, e As EventArgs)
    Dim ChkBoxHeader As CheckBox = CType(CategoriesGridView.HeaderRow.FindControl("chkAll"), CheckBox)
    'Enumerate each GridViewRow
    For Each gvr As GridViewRow In CategoriesGridView.Rows
        'Programmatically access the CheckBox from the TemplateField
        Dim cb As CheckBox = CType(gvr.FindControl("chkShowDiscountImage"), CheckBox)
        If ChkBoxHeader.Checked = True Then
            cb.Checked = True
        Else
            cb.Checked = False             
        End If
    Next
    Save()
End Sub

Protected Sub Save()
    Dim result As Boolean = False
    For Each row As GridViewRow In CategoriesGridView.Rows
        ' Get the Id from the DataKey property.
        Dim cId As Integer = Convert.ToInt32(CategoriesGridView.DataKeys(row.RowIndex).Values(0))

        'Get the checked value of the CheckBox
        Dim showDiscountImage As Boolean = TryCast(row.FindControl("chkShowDiscountImage"), CheckBox).Checked

        result = CatalogServices.Categories.UpdateCategory(cId, showDiscountImage)
    Next

    If result = True Then
        PopulateGridView()
    Else
        'ToDo Display Error of some sort
        Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "Alert", "alert('There was a problem updating the database')", True)
    End If

End Sub
1

There are 1 answers

0
TrevorBrooks On

Instead of using Page_Load use the Page_Init event.