hyperlink.NavigateUrl 在母版页上发生更改(可能通过 ResolveUrl() )

发布于 2024-10-16 07:55:55 字数 617 浏览 4 评论 0 原文

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

hyp1.NavigateUrl = "#notificationPopup";

呈现为:

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

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

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

看起来它可能正在通过 ResolveClientUrl() 或当我在母版页上设置它时运行 navigatorUrl 。我尝试将 替换为 ,但发生了同样的事情。

有什么想法吗?

I have a hyperlink that in certain cases I want to change to show a jquery popup, but I'm having a strange problem when doing it on a master page. The following works in a regular page:

hyp1.NavigateUrl = "#notificationPopup";

Which renders as:

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

This is exactly what I want. The problem is with the exact same code on a hyperlink on the master page it renders as:

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

It looks like it might be running the navigateUrl through ResolveClientUrl() or something when I'm setting it on the master page. I've tried swapping the <asp:hyperlink for a <a href runat=server, but the same thing happens.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

另类 2024-10-23 07:55:55

MSDN 上有一个注释 Control。 ResolveClientUrl方法说明。

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

因此,示例中母版页的行为是完全可预测的(尽管这使用起来不太舒服)。那么还有哪些替代方案呢?

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

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

在这种情况下,如果此控件仅应在服务器端:您可以使用 UriBuilder 类:

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

There is a note on MSDN Control.ResolveClientUrl method description.

The URL returned by this method is
relative to the folder containing the
source file in which the control is
instantiated. Controls that inherit
this property, such as UserControl and
MasterPage, will return a fully
qualified URL relative to the control.

So the behavior of master page in your exampe is fully predictable (although this is not a very comfortable to work with). So what are the alternatives?

The best one is to set the <a> as a client control (remove runat="server"); should work like a charm even in a master page:

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

In the case if this control should be server side only: you could just build an URL from your code behind by using UriBuilder class:

UriBuilder newPath = new UriBuilder(Request.Url);
// this will add a #notificationPopup fragment to the current URL
newPath.Fragment = "notificationPopup";
hyp1.HRef = newPath.Uri.ToString();
面如桃花 2024-10-23 07:55:55

在表单上创建一个隐藏字段,并将值设置为您要导航的位置/超链接的 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";

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

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

Create a hidden field on your form and set the value to where you want to navigate / the url of the hyperlink instead of the hyperlinks navigate url. Then call the onclick method of the hyperlink in javascript and set the hyperlink there before the browser does the actual navigation.

<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>

Code behind would be:
hdnHypNav.value = "#notificationPopup";

You could also just try setting the url after the postback with below code, i.e. replace your code behind line with this one but I am not sure if it will work...

ScriptManager.RegisterStartupScript(this,this.GetType(),"SetHyp","$('ctl00_hyp1').href = '#notificationPopup';",True)
温馨耳语 2024-10-23 07:55:55

我找到了另一种方法来解决这个问题。

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

I found another way to solve the problem.

hyp1.Attributes.Add("href", "#notificationPopup");
迷途知返 2024-10-23 07:55:55

考虑到我用 runat="server" 替换静态超链接的全部原因是为了从基于资源的自动本地化中受益,这些答案都不能满足我的需求。

我的解决方法是将超链接括在文字中:

<asp:Literal ID="lit1" runat="server" meta:resourcekey="lit1">
  <a href="#notificationPopup">Example</a>
</asp:Literal>

缺点是如果您需要以编程方式操作链接,那就有点烦人了:

lit1.Text = String.Format("<a href=\"{0}\">Example</a>", HttpUtility.HtmlAttributeEncode(url));

Seeing as the whole reason I replaced my static hyperlink with a runat="server" one was to benefit from automatic resource-based localization, none of these answers served my needs.

My fix was to enclose the hyperlink in a literal:

<asp:Literal ID="lit1" runat="server" meta:resourcekey="lit1">
  <a href="#notificationPopup">Example</a>
</asp:Literal>

The downside is if you need to programmatically manipulate the link, it's a bit more annoying:

lit1.Text = String.Format("<a href=\"{0}\">Example</a>", HttpUtility.HtmlAttributeEncode(url));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文