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?
views:
1889answers:
6
+6
A:
<a name='<%# Eval("PostId") %>' />
where PostId is the name of the property you want to appear in your anchor.
Travis Collins
2008-10-22 17:50:58
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
2008-10-22 17:51:37
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
2008-10-22 20:15:43
+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
2008-10-22 17:53:47
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
2008-10-22 18:28:45
A:
@Travis
This PostId is known only in PostsRepeater_ItemDataBound event method so how should I reference it?
rafek
2008-10-22 18:05:41
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
2008-10-22 18:12:57
A:
Ok. I've resolved it this way:
<a name='<%# DataBinder.Eval(Container.DataItem, "Id") %>' />
where Id is the property of binded entity.
rafek
2008-10-22 18:41:59
Travis's Eval("Id") is equivalent - it just automatically picks up the Container.DataItem.
Mark Brackett
2008-10-22 18:59:04