简体   繁体   English

在锚标记中使用服务器标记

[英]Use server tag in an anchor tag

I would like to use a property ID value in the anchor tag as below: 我想在锚标记中使用属性ID值,如下所示:

<a id="aExample" href="/test/example.aspx?id=<%# Id %>" runat="server">Example</a>

But when the page is rendered instead of getting the href as "/test/example.aspx?id=5" i am getting "/test/example.aspx?id=<%# Id %>" as plain text assigned as href of the anchor. 但是当呈现页面而不是将href变为"/test/example.aspx?id=5"时,我将"/test/example.aspx?id=<%# Id %>"作为指定为href的纯文本锚。

Id = is a property defined in code behind. Id =是在代码后面定义的属性。

Can anybody help me whats wrong with this? 任何人都可以帮我解决这个问题吗? NB: I need runat="server"` to be present. 注意:我需要runat =“server”`才能出现。

My tag is not inside any Grid view control but within a user control. 我的标记不在任何网格视图控件内,而是在用户控件中。 <%= <(%)=

Try this, 尝试这个,

if you want get variable value from your .cs so you can use. 如果你想从你的.cs获得变量值,那么你可以使用。 Declare variable in your page. 在页面中声明变量。 .cs page .cs页面

 public int Id = 0;

aspx page aspx页面

<a id="aExample" href="/test/example.aspx?id=<%= Id %>" runat="server">Example</a>

and your a tag is inside in gridview control so you can use like... 并且您的标签位于gridview控件内部,因此您可以使用...

<a id="aExample" href="/test/example.aspx?id=<%#Eval("Id")%>" runat="server">Example</a>

Try this 尝试这个

<a id="aExample" href='<%= CompleteURL %>' runat="server">Example</a>

If you can build and bind the complete URL in your property (code behind). 如果您可以构建并绑定属性中的完整URL(代码隐藏)。 I think this can be optimal solution. 我认为这可以是最佳解决方案。 This way you can also change your URL dynamically. 这样,您还可以动态更改URL。

也许你可以试试这个

<asp:HyperLink ID="aExample" runat="server" NavigateUrl='<%# String.Format("/test/example.aspx?id={0}", Eval("Id")) %>'>Example</asp:HyperLink>

你可以在后面的代码中执行此操作(如果它不在Gridview内或其他类似的控件中)

aExample.Href = "/test/example.aspx?id="+ YourEntity.Id;

You can also set the href of anchor tag in CodeBehind . 您还可以在CodeBehind设置锚标记的href

CodeBehind: 代码隐藏:

int Id = 4;
aExample.HRef = "/test/example.aspx?id=" + Id;

Thanks 谢谢

请试试这个,

<a id="aExample" href='<%# string.Format("/test/example.aspx?id={0}", Id) %>'  runat="server">Example</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM