setting ItemTemplate value to DataList item index?

S

Stimp

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
 
W

Weston Weems

I dont have a development machine in front of me at the
moment, but I do know that a lot of other .net
databindable lists have OnItemDataBound, and OnItemCreated
events.

If it has a OnItemDataBound event, it'd be REAL easy.
throw some winforms control in the template, then you can
modify it from the code behind

if(e.ItemType == ListItemType.ListItem){
((LinkButton) e.FindControl("myLinkButton")).LinkUrl =
e.Item.ItemIndex;


}

Something along those lines. I know it works for DataGrid
and I am pretty sure data list too, but I dont think it
works with repeater.
 
G

Guest

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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top