Asp.net hyperlink.NavigateUrl正在母版页上更改(可能是通过ResolveUrl()更改)
我有一个超链接,在某些情况下,我想更改它以显示jquery弹出窗口,但在母版页上执行时遇到了一个奇怪的问题。以下内容适用于常规页面:Asp.net hyperlink.NavigateUrl正在母版页上更改(可能是通过ResolveUrl()更改),asp.net,hyperlink,master-pages,resolveurl,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">
hyp1.NavigateUrl = "#notificationPopup";
其呈现为:
<a id="ctl00_hyp1" href="#notificationPopup">Example</a>
<a id="ctl00_hyp1" href="../MasterPages/#notificationPopup">Example</a>
这正是我想要的。问题在于母版页上的超链接上的代码与呈现的代码完全相同:
<a id="ctl00_hyp1" href="#notificationPopup">Example</a>
<a id="ctl00_hyp1" href="../MasterPages/#notificationPopup">Example</a>
当我在母版页上进行设置时,它可能正在通过ResolveClientUrl()或其他方式运行navigateUrl。我已尝试交换
在表单上创建一个隐藏字段,并将值设置为要导航的位置/超链接的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>
有一个关于MSDN方法说明的注释
此方法返回的URL为
相对于包含
控件所在的源文件
实例化。继承的控件
此属性,例如UserControl和
母版页,将返回一个完整的
相对于控件的限定URL
因此,exampe中母版页的行为是完全可预测的(尽管这不是一个很好的处理方式)。那么,还有什么选择呢
最好的方法是将
设置为客户端控件(删除runat=“server”
);即使在母版页中,也应该像符咒一样工作:
<a href="#notificationPopup">Example</a>
我找到了另一种解决问题的方法
hyp1.Attributes.Add("href", "#notificationPopup");
为重建完整URL的想法欢呼。使用实际的URL+“#notificationPopup”对于弹出窗口效果很好,所以我选择了:Request.URL.ToString()+“#notificationPopup”代码>因为它更简单