Paging DataGrid Checkbox Persistence

I

iforsyth

Have a paging datagrid with a checkbox control in column(0).
ViewState is enabled.
I check the checkbox in first row of the grid on a page and then
the program hits this event:

Private Sub dgRegGrid_PageIndexChanged(ByVal source As Object, ByVal e
As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles
dgRegGrid.PageIndexChanged

I then do a loop to check the checkbox state.

For i = 0 To dgRegGrid.Items.Count - 1
chk = CType(dgRegGrid.Items.Item(i).Cells(0).FindControl("chkAttended"),
CheckBox)
If chk.Checked = True Then
objCustomer.UpdateCheckBoxState(dgRegGrid.DataKeys(i),
"Y")
Else
objCustomer.UpdateCheckBoxState(dgRegGrid.DataKeys(i),
" ")
End If
Next

Oddly, the chk.Checked value comes up FALSE when the row is checked.
I have confirmed that the datakey matches the row being read in at the
time.

Any suggestions on how to get at these check values?

Ian
 
M

Mike Moore [MSFT]

Hi Ian,

Here is a sample that may help you. Note that the check boxes will be reset
to unchecked each time you change pages. The results output is from the
state of the check boxes when you selected to change the page.

**** ASPX
A DataGrid with AutoGenerateColumns turned off and containing one template
column which contains a check box.

<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox id="CheckBox1" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>

**** Code-behind
Imports System.Data.SqlClient

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.AllowPaging = True
DataGrid1.PageSize = 10
DataGrid1.PagerStyle.Mode = PagerMode.NextPrev
DataGrid1.PagerStyle.NextPageText = "Next"
DataGrid1.PagerStyle.PrevPageText = "Prev"
Bind()
End If
End Sub

Private Sub Bind()
Dim MyList As New ArrayList
Dim i As Int32
For i = 0 To 25
MyList.Add(i)
Next
DataGrid1.DataSource = MyList
DataGrid1.DataBind()
End Sub

Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e
As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles
DataGrid1.PageIndexChanged
Dim i As Int32
For i = 0 To DataGrid1.Items.Count - 1
Dim ck As CheckBox
ck = DataGrid1.Items(i).Cells(0).FindControl("CheckBox1")
Response.Write(ck.Checked & "<br>")
Next
DataGrid1.CurrentPageIndex = e.NewPageIndex
Bind()
End Sub


Does this answer your question?

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.

This posting is provided "AS IS", with no warranties, and confers no rights.


--------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top