I am using below code
It is returning correct value of checked check box
but it does not mark checkbox checked while paging
Can you please check where i m wrong ?
Below is my code
My gridview >>
<asp:GridView id="Gridview1"
runat="server" DataSourceID = "SqlDataSource1"
PageSize="30" EmptyDataText = "No Records Found" EmptyDataRowStyle-HorizontalAlign ="Center"
AllowPaging="true" DataKeyNames="EmpID"
HorizontalAlign="Center" BorderColor ="Black" Width = "762" Font-Names="Calibri"
AllowSorting = "true" Visible= "false" AutoGenerateColumns= "false" OnPageIndexChanging ="Gridview1_PageIndexChanging" >
<Columns>
<asp:BoundField DataField="EmpID" HeaderText="EmpID" SortExpression = "EmpID"/>
<asp:BoundField DataField="EmpName" HeaderText="EmpName" SortExpression = "EmpName" />
<asp:BoundField DataField="SectionName" HeaderText="SectionName" SortExpression = "SectionName"/>
<asp:TemplateField HeaderText ="Approved Salary Received" >
<ItemTemplate >
<asp:CheckBox ID="chkStatus" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="SteelBlue" Font-Bold="true" ForeColor="White" Font-Size = "Medium" Font-Names="Calibri" Wrap ="true" Height = "30" CssClass="fixedAtTop" />
<RowStyle BackColor="AliceBlue" Font-Size = "Small" Font-Names="Calibri" />
<AlternatingRowStyle BackColor="White" Font-Size = "Small" Font-Names="Calibri"/>
<PagerSettings
Position="Bottom"
mode = "NumericFirstLast"
LastPageText = "Last Page" />
</asp:GridView>
------------------
Gridview Checkbox code
Protected Sub Gridview1_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles Gridview1.PageIndexChanging
Call SaveCheckedValues()
Gridview1.PageIndex = e.NewPageIndex
'Gridview1.DataSourceID = "SqlDataSource1"
Gridview1.DataBind()
Call PopulateCheckedValues()
End Sub
Private Sub PopulateCheckedValues()
Dim userdetails As ArrayList = DirectCast(Session("CHECKED_ITEMS"), ArrayList)
'Alert.Show(userdetails.Count)
If userdetails IsNot Nothing AndAlso userdetails.Count > 0 Then
For Each gvrow As GridViewRow In Gridview1.Rows
Dim index As Integer = Convert.ToInt32(Gridview1.DataKeys(gvrow.RowIndex).Value)
If userdetails.Contains(index) Then
' Alert.Show(userdetails.Contains(index))
Dim myCheckBox As CheckBox = CType(gvrow.FindControl("chkStatus"), CheckBox)
If myCheckBox IsNot Nothing Then
myCheckBox.Checked = True
myCheckBox.BackColor = Drawing.Color.Red
Alert.Show("true")
Else
Alert.Show("false")
End If
'Gridview1.Rows(i).Attributes.Add("style", "background-color:aqua")
End If
Next
End If
End Sub
'This method is used to save the checkedstate of values
Private Sub SaveCheckedValues()
Dim userdetails As New ArrayList()
Dim index As Integer = -1
For Each gvrow As GridViewRow In Gridview1.Rows
index = Convert.ToInt32(Gridview1.DataKeys(gvrow.RowIndex).Value)
Dim result As Boolean = DirectCast(gvrow.FindControl("chkStatus"), CheckBox).Checked
' Check in the Session
If Session("CHECKED_ITEMS") IsNot Nothing Then
userdetails = DirectCast(Session("CHECKED_ITEMS"), ArrayList)
End If
If result Then
If Not userdetails.Contains(index) Then
userdetails.Add(index)
End If
Else
userdetails.Remove(index)
End If
Next
If userdetails IsNot Nothing AndAlso userdetails.Count > 0 Then
Session("CHECKED_ITEMS") = userdetails
' Alert.Show(userdetails.Count)
Else
' Alert.Show("no")
End If
End Sub