By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,835 Members | 1,346 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,835 IT Pros & Developers. It's quick & easy.

Paging DataGrid Checkbox Persistence

P: n/a
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.DataGridPageChangedEvent Args) 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
Nov 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.DataGridPageChangedEvent Args) 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.
--------------------
From: do***********@yahoo.com (iforsyth)
Newsgroups: microsoft.public.dotnet.framework.aspnet
Subject: Paging DataGrid Checkbox Persistence
Date: 9 Jan 2004 11:50:20 -0800
Organization: http://groups.google.com
Lines: 30
Message-ID: <d8**************************@posting.google.com >
NNTP-Posting-Host: 63.167.0.3
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1073677821 22758 127.0.0.1 (9 Jan 2004 19:50:21 GMT) X-Complaints-To: gr**********@google.com
NNTP-Posting-Date: Fri, 9 Jan 2004 19:50:21 +0000 (UTC)
Path: cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl!TK2MSFTN GXA05.phx.gbl!TK2MSFTNGP08
..phx.gbl!newsfeed00.sul.t-online.de!t-online.de!news-spur1.maxwell.syr.edu!n
ews.maxwell.syr.edu!postnews1.google.com!not-for-mail Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet:201026
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

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.DataGridPageChangedEvent Args) 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


Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.