Access controls on gridview while using paging
I am attempting to get information at postback from a checkbox on a gridview that has multiple pages. Things work fine when there is only one page worth of data, but with paging checkboxes always return false no matter what page you are on if you move to different pages. So for example in my app I have clicked on the page 2 link and selected a checkbox on that page and then posted back. In my handler I iterate through every row and check like this.
For Each row In grdView.Rows
Dim RowCheckBox As CheckBox = CType(row.FindControl(crtl), CheckBox)
If RowCheckBox.Checked Then
sSelectedItem = CType(row.FindControl(sIDFieldName), TextBox).Text
checkedItems.Add(sSelectedItem)
End If
Next
So the information I want is on the 1st row of page 2 (row 6). But none of the checkboxes report being checked. Here is what my gridview is reporting:
? grdView.Rows.Count 5
? grdView.PageCount 2
? grdView.PageSize 5
So even though it knows there are 5 rows per page and 2 pages it does not see more that 5 rows. It also reports the pageindex as 0 (1st page) If开发者_高级运维 I set the PageIndex to 1 (second page) the grdView.Rows.Count = 5 even though there is only 1 row on the second page.
I also get the same return value as the first page despite setting the page number to the second page. I can not figure out how to get anything on a page other than the first one.
I have googled this and suprisingly have not found an answer since this seems to be somthing that would be commonly used to get data. This is a repost of an unanswered question in the C# section, but I thought maybe an answer could be found with a fresh post.
I looked at the checkbox fields and found numbering problems. For instance on page 2 there is one row and the checkbox has a ID of ....$ctl02$... But when you query the checkbox in a loop it gives it's UniqueID as ....$ctl11$... So obviously they are returning false. But if you do a Request.Item with the ID that shows in the page you get the checkbox. With the UniqueID the checkbox spits back you get nothing and hence all the checkboxes' checked properties are false.
Any help would be appreciated.
When you initially set the data to the GridView, you probably did something like this:
grid.DataSource = something
grid.DataBind()
When you databind, the current page worth of data is bound to the grid, nothing more. If you want to access data from another page, you need to reset the datasource, and rebind the data.
Just setting the current page to 2 won't help, because the second page worth of data was never bound to the grid.
精彩评论