eval in anchor tag href

2019-04-10 13:27发布

What I am trying to achieve is to use an Eval as parameter in my anchor tag's href. The anchor is nested inside a repeater, why I cannot use the code behind to achieve this.

I have tried a few things without any luck.

<a href="http://MyWebsite/ActiveUsers?ID=InsertEvalHere"><%# Eval("Name")%></a>

The following code below is what I have tried to do:

<a href="<% "http://MyWebsite/ActiveUsers?ID=" + DataBinder.Eval(Container.DataItem("ID"))%>"><%# Eval("Name")%></a>

<a href="<% "http://MyWebsite/ActiveUsers?ID=" + Eval("ID")%>"><%# Eval("Name")%></a>

<a href="http://MyWebsite/ActiveUsers?ID=<% DataBinder.Eval(Container.DataItem("ID"))%>"><%# Eval("Name")%></a>

<a href="http://MyWebsite/ActiveUsers?ID=<%# Eval("ID")%>"><%# Eval("Name")%></a>

None of the above seemed to be right, as I keep getting this error - The tag is not well formed.

How should I handle this?

5条回答
SAY GOODBYE
2楼-- · 2019-04-10 13:55
<a href="<%# String.Format("http://MyWebsite/ActiveUsers?ID={0}", Eval("ID")) %>">
查看更多
聊天终结者
3楼-- · 2019-04-10 14:06

Use this:

<a href='http://MyWebsite/ActiveUsers?ID=<%# Eval(Container.DataItem("ID"))%>'><%# Eval("Name")%></a>
查看更多
冷血范
4楼-- · 2019-04-10 14:06

Use single quotation and move the url out like this

<a href='http://MyWebsite/ActiveUsers?ID=<% DataBinder.Eval(Container.DataItem("ID"))%>'><%# Eval("Name")%></a> 
查看更多
Juvenile、少年°
5楼-- · 2019-04-10 14:06

a bit late but....

<a href='<%# Eval("ID","http://MyWebsite/ActiveUsers?ID={0}") %>'><%# Eval("Name")%></a>
查看更多
等我变得足够好
6楼-- · 2019-04-10 14:13

If you need access the anchor in code behind for enabling and disabling it based on condition at datalist_ItemDataBound , then you can use the way provided by Nicky Waites with a small change as shown below

<a id="register" runat="server" href='<%# String.Format("http://MyWebsite/ActiveUsers?ID={0}", Eval("ID")) %>'>

Hope this would help someone.

查看更多
登录 后发表回答