total items in paged output

T

Trapulo

Hello,
I've a gridview binded to an objectdataSource. Custom paging is enabled and
my objectdataSource is using a SelectCountMethod to retrieve the number of
total retrieved rows when it's showing only 25 rows at time (the rows that
the SelectMethd returns).
All works fine, but I need to show a label as "Total items: xxxx" on the
page, where xxxx is the total rows the select can retrieve (the value the
gridview uses to calculate total available pages number). I cannot make
this, can someone help me?

thanks
 
W

Walter Wang [MSFT]

Hi Trapulo,

When you enabled EnablePaging for the ObjectDataSource, its Selected Event
will be called twice, one for the SelectMethod, one for the
SelectCountMethod. The event parameter ObjectDataSourceStatusEventArgs has
a property named ReturnValue will be the return value from the
SelectMethod/SelectCountMethod. Since the SelectMethod will return an
enumerable list and SelectCountMethod returns int, you can check for the
data type and get the returned record count:


protected void ObjectDataSource1_Selected(object sender,
ObjectDataSourceStatusEventArgs e)
{
if (e.ReturnValue is int)
{
lblCount.Text = ((int)e.ReturnValue).ToString();
}
}



Hope this helps.


By the way, if you're selecting from database, you may find following tip
useful to improve performance:


#Update: GridView Custom Paging with ObjectDataSource
http://www.unboxedsolutions.com/sean/archive/2006/01/21/843.aspx


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

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

Trapulo

Great!
I didn't know this behavor! This solve my problem!

I'm not sure the System.Web.HttpContext.Current.Items solution the url you
suggested shows is better than mine.
My class has a global variable:
Private _totalRows As Int32

and the "select" method make this:

_totalRows = [total rows count from db]

then, the "selectcount" method is only a row:

Return _totalRows



I think my pattern has same advantages and doesn't involve httpcontent
collection, is this right? And it's early binded ;-)

thanks for your email too: I was out of office last days and I didn't
remember to check the NG.
 
W

Walter Wang [MSFT]

Hi Trapulo,

Thanks for the confirmation.

A DataObject class can use static functions as SelectMethod and
SelectCountMethod, in that case, I think using HttpContext.Current.Items is
better since it avoids using static variable (which is not good since a
static variable will last in the entire life cycle as Asp.net worker
process).

In summary, for non-static SelectMethod/SelectCountMethod, I think using
either HttpContext.Current.Items or a member variable should both work. I
think the performance difference here will be trivial. I would perfer the
HttpContext.Current.Items approach since it avoids using a member variable.
Normally I will keep the DataObject class free of member variables.
However, this is only a personal taste.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

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

Trapulo

Thank you.
So I have a doubt: the ObjectDataSource creates a new instance of DataObject
when it needs to make a Select, and than it disposes that instance when it
has completed data retrieval (or insert, or update, etc.), doesn't it? If
this is the real behavor, I think that the static variable doesn't require
more resources that HttContext because all two solutions requires memory and
resources that release after the page has been rendered (at least). This is
wrong if I have a wrong idea of ObjectDataSource related lifecycles.

In fact I define al DataObject methods as Shared, but Select and SelectCount
not, so they can share the static variable about last row count.
 
W

Walter Wang [MSFT]

Hi Trapulo,

A static variable in a class doesn't bind to an instance, it's shared by
all instances of the class. Actually, if it's public, you can even access
the static variable without creating an instance of the class. The static
variable will have the same life cycle as the AppDomain. For asp.net, this
means it will live as long as the worker process is alive. You can verify
this by writing/reading a static variable in a class from two WebForm or
two postbacks.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

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

Trapulo

I'm sorry, there was a mistake explaining my solution. In my post 7
september, I wrote "global variable" : I don't use a static variable
(private shared count as int32), but a class variable (private _count as
int32 at class level).
Then in your reply at 10 september, you wrote "static variable" and I didn't
notice that (I'm sorry, I'm a VB developer and I I'm used to read "shared"
not "static" so I was not so obvious what static means).
With "global" I didn't mean "static", but only a class-scope variable. So,
using a standard variable and not a shared (static) I think that things are
different.

thanks
 
W

Walter Wang [MSFT]

Hi Trapulo,

Thanks for your clarification. I'm also sorry for the inconsistent terms
when describing the static/shared variables.

So I assume this issue can be closed now? Let me know if you have anything
else unclear. Thanks.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top