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?
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();