Determining whether the current item is the last one from inside ItemDataBound

Discussion in 'ASP .Net Datagrid Control' started by Nathan Sokalski, May 21, 2006.

  1. I have a control that I want displayed in all items except the last one. I
    figured the best way to do this was to determine whether the current item
    was the last from within the ItemDataBound event using code such as the
    following:


    If e.Item.ItemIndex=(numberofitems-1) Then mycontrol.Enabled=False


    but I cannot find a property that contains the total number of items before
    the ItemDataBound event is raised. Any ideas? Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
    Nathan Sokalski, May 21, 2006
    #1
    1. Advertising

  2. Nathan,

    In almost all collection there is a count or a length telling what is the
    total number of the items

    The count-1 or lenght - 1 is than the index to the last.

    I hope this helps,

    Cor

    "Nathan Sokalski" <> schreef in bericht
    news:...
    >I have a control that I want displayed in all items except the last one. I
    >figured the best way to do this was to determine whether the current item
    >was the last from within the ItemDataBound event using code such as the
    >following:
    >
    >
    > If e.Item.ItemIndex=(numberofitems-1) Then mycontrol.Enabled=False
    >
    >
    > but I cannot find a property that contains the total number of items
    > before the ItemDataBound event is raised. Any ideas? Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    Cor Ligthert [MVP], May 21, 2006
    #2
    1. Advertising

  3. That would help, except for one thing. What collection do I use? I tried the
    following two:

    DataList.DataKeys.Count
    DataList.Items.Count

    The problem is that these collections are not completely filled until after
    the ItemDataBound event has been called however many times it gets called.
    To see this, add a line such as:

    Response.Write(DataList.DataKeys.Count)

    inside the ItemDataBound event handler, and you will see that the value is
    different each time it is called, which makes it almost useless inside this
    handler. I need a property or method that can tell me the number of items
    before the DataBind() method is called. I thought about using the DataTable
    that I use for the DataList.DataSource property, but that would require me
    to create a public or session variable, which is not the ideal way to do it,
    and it requires extra code. Any other ideas? Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Cor Ligthert [MVP]" <> wrote in message
    news:...
    > Nathan,
    >
    > In almost all collection there is a count or a length telling what is the
    > total number of the items
    >
    > The count-1 or lenght - 1 is than the index to the last.
    >
    > I hope this helps,
    >
    > Cor
    >
    > "Nathan Sokalski" <> schreef in bericht
    > news:...
    >>I have a control that I want displayed in all items except the last one. I
    >>figured the best way to do this was to determine whether the current item
    >>was the last from within the ItemDataBound event using code such as the
    >>following:
    >>
    >>
    >> If e.Item.ItemIndex=(numberofitems-1) Then mycontrol.Enabled=False
    >>
    >>
    >> but I cannot find a property that contains the total number of items
    >> before the ItemDataBound event is raised. Any ideas? Thanks.
    >> --
    >> Nathan Sokalski
    >>
    >> http://www.nathansokalski.com/
    >>

    >
    >
    Nathan Sokalski, May 21, 2006
    #3
  4. Nathan,

    I always use the datasource to get the total items.

    Be aware that you take the defaultview if you use that, which by instance in
    a sort becomes than dynamic.

    Cor

    "Nathan Sokalski" <> schreef in bericht
    news:...
    > That would help, except for one thing. What collection do I use? I tried
    > the following two:
    >
    > DataList.DataKeys.Count
    > DataList.Items.Count
    >
    > The problem is that these collections are not completely filled until
    > after the ItemDataBound event has been called however many times it gets
    > called. To see this, add a line such as:
    >
    > Response.Write(DataList.DataKeys.Count)
    >
    > inside the ItemDataBound event handler, and you will see that the value is
    > different each time it is called, which makes it almost useless inside
    > this handler. I need a property or method that can tell me the number of
    > items before the DataBind() method is called. I thought about using the
    > DataTable that I use for the DataList.DataSource property, but that would
    > require me to create a public or session variable, which is not the ideal
    > way to do it, and it requires extra code. Any other ideas? Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    > "Cor Ligthert [MVP]" <> wrote in message
    > news:...
    >> Nathan,
    >>
    >> In almost all collection there is a count or a length telling what is the
    >> total number of the items
    >>
    >> The count-1 or lenght - 1 is than the index to the last.
    >>
    >> I hope this helps,
    >>
    >> Cor
    >>
    >> "Nathan Sokalski" <> schreef in bericht
    >> news:...
    >>>I have a control that I want displayed in all items except the last one.
    >>>I figured the best way to do this was to determine whether the current
    >>>item was the last from within the ItemDataBound event using code such as
    >>>the following:
    >>>
    >>>
    >>> If e.Item.ItemIndex=(numberofitems-1) Then mycontrol.Enabled=False
    >>>
    >>>
    >>> but I cannot find a property that contains the total number of items
    >>> before the ItemDataBound event is raised. Any ideas? Thanks.
    >>> --
    >>> Nathan Sokalski
    >>>
    >>> http://www.nathansokalski.com/
    >>>

    >>
    >>

    >
    >
    Cor Ligthert [MVP], May 21, 2006
    #4
  5. Why don't you use rather the PreRender event, where all items and the Items
    collection are already fully built?

    Eliyahu

    "Nathan Sokalski" <> wrote in message
    news:...
    >I have a control that I want displayed in all items except the last one. I
    >figured the best way to do this was to determine whether the current item
    >was the last from within the ItemDataBound event using code such as the
    >following:
    >
    >
    > If e.Item.ItemIndex=(numberofitems-1) Then mycontrol.Enabled=False
    >
    >
    > but I cannot find a property that contains the total number of items
    > before the ItemDataBound event is raised. Any ideas? Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    Eliyahu Goldin, May 21, 2006
    #5
  6. Thanks, I don't think that idea ever occurred to me because I have never
    used the PreRender event before, but I think that the fact that the
    condition is only true for one item, it is definitely more efficient.
    Thanks!
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Eliyahu Goldin" <> wrote in message
    news:...
    > Why don't you use rather the PreRender event, where all items and the
    > Items collection are already fully built?
    >
    > Eliyahu
    >
    > "Nathan Sokalski" <> wrote in message
    > news:...
    >>I have a control that I want displayed in all items except the last one. I
    >>figured the best way to do this was to determine whether the current item
    >>was the last from within the ItemDataBound event using code such as the
    >>following:
    >>
    >>
    >> If e.Item.ItemIndex=(numberofitems-1) Then mycontrol.Enabled=False
    >>
    >>
    >> but I cannot find a property that contains the total number of items
    >> before the ItemDataBound event is raised. Any ideas? Thanks.
    >> --
    >> Nathan Sokalski
    >>
    >> http://www.nathansokalski.com/
    >>

    >
    >
    Nathan Sokalski, May 21, 2006
    #6
    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. samir dsf
    Replies:
    4
    Views:
    418
    Siva M
    Sep 10, 2005
  2. news.amnet.net.au

    Determining whether String Array is empty

    news.amnet.net.au, Sep 7, 2004, in forum: Java
    Replies:
    5
    Views:
    28,064
    Mark Wright
    Sep 8, 2004
  3. Nathan Sokalski
    Replies:
    5
    Views:
    3,002
    Nathan Sokalski
    May 21, 2006
  4. QUASAR
    Replies:
    6
    Views:
    408
    QUASAR
    Jan 17, 2004
  5. Nathan Sokalski
    Replies:
    5
    Views:
    127
    Nathan Sokalski
    May 21, 2006
Loading...

Share This Page