在主页面上更改了hyperlink.NavigateUrl(可能是ResolveUrl())

时间:2011-02-09 12:59:25

标签: asp.net hyperlink master-pages resolveurl

我有一个超链接,在某些情况下我想要更改以显示一个jquery弹出窗口,但我在母版页上执行它时遇到了一个奇怪的问题。以下内容适用于常规页面:

hyp1.NavigateUrl = "#notificationPopup";

呈现为:

<a id="ctl00_hyp1" href="#notificationPopup">Example</a>

这正是我想要的。问题在于它呈现的主页面上的超链接完全相同的代码:

<a id="ctl00_hyp1" href="../MasterPages/#notificationPopup">Example</a>

当我在母版页上设置它时,看起来它可能正在通过ResolveClientUrl()运行navigateUrl。我已经尝试将<asp:hyperlink换成<a href runat=server,但同样的事情发生了。

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

有关MSDN Control.ResolveClientUrl方法说明的说明。

  

此方法返回的URL是   相对于包含该文件夹的文件夹   控件所在的源文件   实例化。继承的控件   这个属性,比如UserControl和   MasterPage,将完全返回   相对于控件的限定URL。

因此,您的示例中的母版页的行为是完全可预测的(尽管这不是一个非常舒适的工作)。那么有哪些替代方案?

最好的方法是将<a>设置为客户端控件(删除runat="server");即使在母版页中也应该像魅力一样:

<a href="#notificationPopup">Example</a>

如果此控件仅应为服务器端:您可以使用UriBuilder类从后面的代码构建URL:

UriBuilder newPath = new UriBuilder(Request.Url);
// this will add a #notificationPopup fragment to the current URL
newPath.Fragment = "notificationPopup";
hyp1.HRef = newPath.Uri.ToString();

答案 1 :(得分:0)

在表单上创建一个隐藏字段,并将值设置为您要导航的位置/超链接的url而不是超链接导航URL。然后在javascript中调用超链接的onclick方法,并在浏览器进行实际导航之前设置超链接。

<html><head><title></title></head>
<script type="text/javascript">

function navHyperlink(field)
{
field.href = document.getElementById('ctl00_hdnHypNav').value;
return true;
}

</script>
<input type="hidden" id="hdnHypNav" value="test2.html" runat="server"/>
    <a href="" onclick="navHyperlink(this);" >click here</a>
</html>

背后的代码是:     hdnHypNav.value =“#notificationPopup”;

您也可以尝试使用以下代码尝试在回发后设置网址,即使用此代码替换您的代码,但我不确定它是否可行...

ScriptManager.RegisterStartupScript(this,this.GetType(),"SetHyp","$('ctl00_hyp1').href = '#notificationPopup';",True)

答案 2 :(得分:0)

我找到了解决问题的另一种方法。

hyp1.Attributes.Add("href", "#notificationPopup");