Saturday, March 31, 2012

setting ItemTemplate value to DataList item index?

I have a datalist that outputs a link to a picture in each ItemTemplate:
e.g.
<asp:DataList id="DataList1" ...etc>
<ItemTemplate>
<a href='Pic.aspx?pic=somenumber'>picture</a>
</ItemTemplate>
<asp:DataList>
What I want to do is set the "pic=somenumber" querystring value
to the current DataList item's index.
e.g. something like:
Pic.aspx?pic=<%=DataList1.Items.Item.ItemIndex%>
This returns an error.
How can I retrieve the index of the current Item as it's loading the
template?
I could also create my own index value, that increments as the
DataList adds another ItemTemplate.. but not sure how to do this.
Thanks!
Peter
--
"I hear ma train a comin'
... hear freedom comin"Peter,
Two ways:
1. Use '<%# container.itemindex %>'
2. Create a public function on the page that bumps and returns a static
counter, e.g. (in vb)
Private m_Num As Integer = -1
Public Function NextNum() As Integer
m_Num += 1
Return m_Num
End Function
then...
'<%# NextNum() %>'
The first is easier, the second more extensible.
hth,
Bill
"Stimp" wrote:

> I have a datalist that outputs a link to a picture in each ItemTemplate:
> e.g.
> <asp:DataList id="DataList1" ...etc>
> <ItemTemplate>
> <a href='Pic.aspx?pic=somenumber'>picture</a>
> </ItemTemplate>
> <asp:DataList>
> What I want to do is set the "pic=somenumber" querystring value
> to the current DataList item's index.
> e.g. something like:
> Pic.aspx?pic=<%=DataList1.Items.Item.ItemIndex%>
> This returns an error.
> How can I retrieve the index of the current Item as it's loading the
> template?
> I could also create my own index value, that increments as the
> DataList adds another ItemTemplate.. but not sure how to do this.
> Thanks!
> Peter
> --
> "I hear ma train a comin'
> ... hear freedom comin"
>
P.S. Per Weston's comment, I too prefer to do most things of any complexity
in the code-behind. What he suggested will work great.
"Bill Borg" wrote:
> Peter,
> Two ways:
> 1. Use '<%# container.itemindex %>'
> 2. Create a public function on the page that bumps and returns a static
> counter, e.g. (in vb)
> Private m_Num As Integer = -1
> Public Function NextNum() As Integer
> m_Num += 1
> Return m_Num
> End Function
> then...
> '<%# NextNum() %>'
> The first is easier, the second more extensible.
> hth,
> Bill
> "Stimp" wrote:
>
On Fri, 10 Sep 2004 Bill Borg <BillBorg@.discussions.microsoft.com> wrote:
> Peter,
> Two ways:
> 1. Use '<%# container.itemindex %>'
That's EXACTLY what I was looking for!
Thanks to all who replied!
--
"I hear ma train a comin'
... hear freedom comin"

0 comments:

Post a Comment