eval in anchor tag href

ぐ巨炮叔叔 提交于 2019-12-06 06:21:21

问题


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?


回答1:


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



回答2:


Use this:

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



回答3:


Use single quotation and move the url out like this

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



回答4:


a bit late but....

<a href='<%# Eval("ID","http://MyWebsite/ActiveUsers?ID={0}") %>'><%# Eval("Name")%></a>



回答5:


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.



来源:https://stackoverflow.com/questions/5536956/eval-in-anchor-tag-href

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!