开发者

hyperlink.NavigateUrl getting changed on master page (possibly by ResolveUrl() )

I have a hyperlink that in certain cases I want to change to show a jquery popup, but I'm having a strange probl开发者_如何学JAVAem 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();


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)


I found another way to solve the problem.

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


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));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜