PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET Determining whether the current item is the last one from inside ItemDataBound

Reply

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

 
Thread Tools Rate Thread
Old 21-05-2006, 03:17 AM   #1
Nathan Sokalski
Guest
 
Posts: n/a
Default Determining whether the current item is the last one from inside ItemDataBound


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
njsokalski@hotmail.com
http://www.nathansokalski.com/


  Reply With Quote
Old 21-05-2006, 05:33 AM   #2
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default Re: Determining whether the current item is the last one from inside ItemDataBound

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" <njsokalski@hotmail.com> schreef in bericht
news:OI4c3yHfGHA.5104@TK2MSFTNGP04.phx.gbl...
>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
> njsokalski@hotmail.com
> http://www.nathansokalski.com/
>



  Reply With Quote
Old 21-05-2006, 06:15 AM   #3
Nathan Sokalski
Guest
 
Posts: n/a
Default Re: Determining whether the current item is the last one from inside ItemDataBound

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
njsokalski@hotmail.com
http://www.nathansokalski.com/

"Cor Ligthert [MVP]" <notmyfirstname@planet.nl> wrote in message
news:uhje99IfGHA.3488@TK2MSFTNGP02.phx.gbl...
> 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" <njsokalski@hotmail.com> schreef in bericht
> news:OI4c3yHfGHA.5104@TK2MSFTNGP04.phx.gbl...
>>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
>> njsokalski@hotmail.com
>> http://www.nathansokalski.com/
>>

>
>



  Reply With Quote
Old 21-05-2006, 08:03 AM   #4
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default Re: Determining whether the current item is the last one from inside ItemDataBound

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" <njsokalski@hotmail.com> schreef in bericht
news:e4RNGWJfGHA.3456@TK2MSFTNGP05.phx.gbl...
> 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
> njsokalski@hotmail.com
> http://www.nathansokalski.com/
>
> "Cor Ligthert [MVP]" <notmyfirstname@planet.nl> wrote in message
> news:uhje99IfGHA.3488@TK2MSFTNGP02.phx.gbl...
>> 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" <njsokalski@hotmail.com> schreef in bericht
>> news:OI4c3yHfGHA.5104@TK2MSFTNGP04.phx.gbl...
>>>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
>>> njsokalski@hotmail.com
>>> http://www.nathansokalski.com/
>>>

>>
>>

>
>



  Reply With Quote
Old 21-05-2006, 09:29 AM   #5
Eliyahu Goldin
Guest
 
Posts: n/a
Default Re: Determining whether the current item is the last one from inside ItemDataBound

Why don't you use rather the PreRender event, where all items and the Items
collection are already fully built?

Eliyahu

"Nathan Sokalski" <njsokalski@hotmail.com> wrote in message
news:OI4c3yHfGHA.5104@TK2MSFTNGP04.phx.gbl...
>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
> njsokalski@hotmail.com
> http://www.nathansokalski.com/
>



  Reply With Quote
Old 21-05-2006, 06:07 PM   #6
Nathan Sokalski
Guest
 
Posts: n/a
Default Re: Determining whether the current item is the last one from inside ItemDataBound

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
njsokalski@hotmail.com
http://www.nathansokalski.com/

"Eliyahu Goldin" <removemeegoldin@monarchmed.com> wrote in message
news:OS4GmhKfGHA.1260@TK2MSFTNGP05.phx.gbl...
> Why don't you use rather the PreRender event, where all items and the
> Items collection are already fully built?
>
> Eliyahu
>
> "Nathan Sokalski" <njsokalski@hotmail.com> wrote in message
> news:OI4c3yHfGHA.5104@TK2MSFTNGP04.phx.gbl...
>>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
>> njsokalski@hotmail.com
>> http://www.nathansokalski.com/
>>

>
>



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off