动态设置内联的超链接控件的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);
}
}
更新:
UPDATE:
现在@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屋!