内联动态设置超链接控件的 NavigateUrl 属性 [英] Dynamically set a hyperlink control's NavigateUrl property inline
问题描述
如何在 aspx 页面中内联动态设置独立的(不在 gridview 中)超链接控件的 NavigateUrl 属性?
How can I dynamically set a stand alone(not in gridview) hyperlink control's NavigateUrl property inline in the aspx page?
我曾尝试执行以下操作,但没有奏效.
I have tried to do the following, but it did not work.
<asp:HyperLink id="MyLink"
NavigateUrl="../mypage.aspx?id=<%= pageid %>"
runat="server">My Page</asp:HyperLink>
推荐答案
您可以在代码隐藏中执行此操作:
You could do this in the codebehind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string pageid = "123";
MyLink.NavigateUrl = string.Format("../mypage.aspx?id={0}", pageid);
}
}
<小时>
更新:
既然@Marko Ivanovski 在评论中指出这个超链接位于 GridView
内,我一开始没有注意到,最简单的方法是使用数据绑定 (<%#
语法):
Now that @Marko Ivanovski pointed me in the comments that this hyperlink is inside a GridView
which I didn't notice in the beginning the easiest would be to use databinding (<%#
syntax):
<asp:TemplateColumn>
<ItemTemplate>
<asp:HyperLink
id="MyLink"
NavigateUrl='<%# Eval("pageid", "~/mypage.aspx?id={0}") %>'
runat="server">
My Page
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>
在这种情况下,pageid
是数据源的一个属性.
In this case pageid
is a property of the data source.
更新 2:
您真的需要服务器端控件吗?怎么样:
Do you really need a server side control? How about:
<a href="<%= this.ResolveUrl("~/mypage.aspx?id=" + pageid) %>">
My Page
</a>
这篇关于内联动态设置超链接控件的 NavigateUrl 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!