total items in paged output

Discussion in 'ASP .Net Web Controls' started by Trapulo, Aug 31, 2007.

  1. Trapulo

    Trapulo Guest

    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
    Trapulo, Aug 31, 2007
    #1
    1. Advertising

  2. 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 (, 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.
    Walter Wang [MSFT], Sep 3, 2007
    #2
    1. Advertising

  3. Trapulo

    Trapulo Guest

    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.





    ""Walter Wang [MSFT]"" <> wrote in message
    news:...
    > 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 (, 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.
    >
    Trapulo, Sep 7, 2007
    #3
  4. 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 (, 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.
    Walter Wang [MSFT], Sep 10, 2007
    #4
  5. Trapulo

    Trapulo Guest

    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.



    ""Walter Wang [MSFT]"" <> wrote in message
    news:...
    > 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 (, 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.
    >
    Trapulo, Sep 10, 2007
    #5
  6. 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 (, 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.
    Walter Wang [MSFT], Sep 11, 2007
    #6
  7. Trapulo

    Trapulo Guest

    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



    ""Walter Wang [MSFT]"" <> wrote in message
    news:...
    > 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 (, 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.
    >
    Trapulo, Sep 11, 2007
    #7
  8. 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 (, 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.
    Walter Wang [MSFT], Sep 12, 2007
    #8
  9. Trapulo

    Trapulo Guest

    All right, close it :)

    Thanks


    ""Walter Wang [MSFT]"" <> wrote in message
    news:D...
    > 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 (, 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.
    >
    Trapulo, Sep 12, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. buran
    Replies:
    0
    Views:
    353
    buran
    Jul 2, 2003
  2. Andreas Pardeike
    Replies:
    7
    Views:
    977
  3. Replies:
    2
    Views:
    7,316
    Bruno Alexandre
    Aug 23, 2006
  4. Paulo

    Total items

    Paulo, May 19, 2008, in forum: ASP .Net
    Replies:
    3
    Views:
    368
    David Wier
    May 19, 2008
  5. Richard Pimentel

    Explorer navigation errors with non-paged or large paged DataGrid.

    Richard Pimentel, Dec 31, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    162
    Richard Pimentel
    Dec 31, 2003
Loading...

Share This Page