views:

1889

answers:

6

I'd like to make anchors to every post in my asp.net forum. Every forum's post is rendered using repeater control. How can I render <a name="anchor_name"></a> in asp.net?

+6  A: 
<a name='<%# Eval("PostId") %>' />

where PostId is the name of the property you want to appear in your anchor.

Travis Collins
A: 

Adding <a name="anchor_name"></a> in the ItemTempate of the repeater at the appropriate spot should do the trick. A little more information might help.

mspmsp
Well of course you're going to put the ID in the name attribute! I don't get why I get a -1. We were given NO information.
mspmsp
+1  A: 

This won't be exact code as I'm not in VS to ensure the syntax but something like this should get you were you want to go.

<a name="<%# Bind('PostId') %>" runat="server" />
Chris Porter
You don't need two-way binding here. Eval is sufficient.
Forgotten Semicolon
I thought about that after I submitted. You shouldn't need runat either, but I tend to use both when testing and then remove what it didn't need.
Chris Porter
A: 

@Travis

This PostId is known only in PostsRepeater_ItemDataBound event method so how should I reference it?

rafek
A: 

Extend the System.Web.UI.WebControls.HyperLink class, and override UniqueID property to return the actual ID:

override string UniqueID { get { return ID; }  }

Use this new user control in the item template of the repeater.

<MyPrefix:MyHyperLink ID="IDOfYourHyperLink" ... />

On ItemDataBound do:

(e.Item.FindControl("IDOfYourHyperLink") as MyHyperlink).ID = NowIKnowWhatToUseHere;
Serhat Özgel
A: 

Ok. I've resolved it this way:

<a name='<%# DataBinder.Eval(Container.DataItem, "Id") %>' />

where Id is the property of binded entity.

rafek
Travis's Eval("Id") is equivalent - it just automatically picks up the Container.DataItem.
Mark Brackett