hyperlink.NavigateUrl 在母版页上发生更改(可能通过 ResolveUrl() )
我有一个超链接,在某些情况下我想更改它以显示 jquery 弹出窗口,但在母版页上执行此操作时遇到了一个奇怪的问题。以下内容在常规页面中有效:
hyp1.NavigateUrl = "#notificationPopup";
呈现为:
<a id="ctl00_hyp1" href="#notificationPopup">Example</a>
这正是我想要的。问题在于母版页上的超链接上的完全相同的代码,它呈现为:
<a id="ctl00_hyp1" href="../MasterPages/#notificationPopup">Example</a>
看起来它可能正在通过 ResolveClientUrl() 或当我在母版页上设置它时运行 navigatorUrl 。我尝试将 ,但发生了同样的事情。
有什么想法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
MSDN 上有一个注释
Control。 ResolveClientUrl
方法说明。因此,示例中母版页的行为是完全可预测的(尽管这使用起来不太舒服)。那么还有哪些替代方案呢?
最好的方法是将
设置为客户端控件(删除
runat="server"
);即使在母版页中也应该像魅力一样工作:在这种情况下,如果此控件仅应在服务器端:您可以使用
UriBuilder
类:There is a note on MSDN
Control.ResolveClientUrl
method description.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 (removerunat="server"
); should work like a charm even in a master page: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:在表单上创建一个隐藏字段,并将值设置为您要导航的位置/超链接的 url,而不是超链接导航 url。然后在浏览器进行实际导航之前,调用 javascript 中超链接的 onclick 方法并在那里设置超链接。
背后的代码是:
hdnHypNav.value = "#notificationPopup";
您也可以尝试使用下面的代码在回发后设置 url,即用此代码替换行后面的代码,但我不确定它是否会起作用...
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.
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...
我找到了另一种方法来解决这个问题。
I found another way to solve the problem.
考虑到我用
runat="server"
替换静态超链接的全部原因是为了从基于资源的自动本地化中受益,这些答案都不能满足我的需求。我的解决方法是将超链接括在文字中:
缺点是如果您需要以编程方式操作链接,那就有点烦人了:
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:
The downside is if you need to programmatically manipulate the link, it's a bit more annoying: