Gridview Paging via ObjectDataSource: Why is maximumRows being set to -1?
So before I tried custom gridview paging via ObjectDataSource... I think I read every tutorial known to man just to be sure I got it. It didn't look like rocket science.
I've set the AllowPaging = True on my gridview.
I've specified PageSize="10" on my gridview.
I've set EnablePaging="True" on the ObjectDataSource.
I've added the 2 paging parms (maximumRows & startRowIndex) to my business object's select method.
I've created an anal开发者_如何学Cogous "count" method with the same signature as the select method.
The only problem I seem to have is during execution... the ObjectDataSource is supplying my business object with a maximumRows value of -1 and I can't for the life of me figure out why. I've searched to the end of the web for anyone else having this problem and apparently I'm the only one. The StartRowIndex parameter seems to be working just fine.
Any ideas?
If SelectCountMethod
is not specified the PageSize
will be -1.
I think you might be a bit confused (I was reading this) between pages and rows.
The parameters used by ObjectDataSource (ODS) for paging are the properties startRowIndex
and maximumRows
but in your function you call them PageIndex
and PageSize
.
ODS displays in pages but uses row numbers for displaying the pages. For example, which a page size of ten (the default) the first call to your GetProducts()
function will have startRowIndex = 0
and maximumRows = 10
- that is start at row zero and provide ten rows.
If the user selects page 2, then GetProducts is called with startRowIndex=10 and maximumRows = 10
. For page 3 this would be startRowIndex = 20
and maximumRows = 10
I suggest you rename your parameters to use the same names - it will make coding easier.
You are not alone. I have the same problem. My setup is a bit different. In my case I am using a ListView instead of a GridView Control. I got 2 pages, one using a ListView and DataPager and the other using a ListView and a Custom Navigation Control(this is in essence the same as the DataPager just different Markup output). Both pages use the same BLL Method and set the maximumRow & startRow in the same way. Basically copy&pasted code.
The ListView-DataPager setup works fine, parameters are set correctly in the BLL Method. The page without DataPager fails. But the DataPager can't be the reason. Both (DataPager and my Custom Control) produce the same, expected values which are passed to the ObjectDataSource parameter collection.
The most confusing thing is, that the SelectCountMethod, which is called after the SelectMethod, gets the correct parameters in both versions!
I was able to work around this problem by setting the parameter values in the OnSelecting Event of the ObjectDataSource:
protected void ObjectDataSource_MyListing_OnSelecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
e.Arguments.StartRowIndex = m_startRowIndex;
e.Arguments.MaximumRows = m_PageSize;
}
I read somewhere that the -1 value for MaximumRows just means "all Remaining Records". So it wouldn't be an error but a default value.
This is my first post here, hope I didn't do anything wrong since this isn't really a solution. Also I don't want to hijack but I would appreciate any additional information ... this problem is bugging me.
You need to set the SelectCountMethod property in the ObjectDataSource control, also set the pagersize and index there, hopefully this will resolve the issue.
Regards, Joy
This work around is working.
I was also stuck up with the same problem.
When you make EnablePaging = "true" then StartRowIndex was getting set to the page size.
If EnablePaging = "false" then MaximumRows was set to zero.
However if you set the
e.Arguments.StartRowIndex = m_startRowIndex;
e.Arguments.MaximumRows = m_PageSize;
Then it is working properly, However this is a workaround
精彩评论