2

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.

netwisedev
  • 41
  • 7
  • can you show some code and explain a bit more? i dont see why anybody cannot access anything in his current page while using a gridview – naveen Aug 12 '11 at 01:54
  • OK - Above is the loop I use. If the checkbox is checked I get a hidden field with the GUID ID of the record in the database. But when you switch pages all the checkboxes return false. I inserted above more info on how it seems the UniqueID for the checkbox is not correct, best I can tell. – netwisedev Aug 12 '11 at 17:57
  • Since it does not appear there will be a resolution to this my workaround was to get the valid information by using Javascript to iterate through the controls on the page with something like: document.getElementsByTagName("input"); and then find out if it is a checkbox: if (oCheckbox.id.indexOf(sFieldID) > 0) and finding the row and then using Request.Form.Item() in my postback to get other information in the row. This is not pretty but gets the info. – netwisedev Aug 19 '11 at 15:51

1 Answers1

0

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.

Erix
  • 7,059
  • 2
  • 35
  • 61
  • Thanks - but this is not a databinding problem, I am referring to getting the data during postback. I set the page correctly, I see p.2 with its one record and check the box on it and click the button to post back. Now I am trying to determine what checkbox was checked and none of them have the check property set to true. So all the information above is dealing with the handler for my postback. – netwisedev Aug 12 '11 at 15:32