Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NavigateUrl="#" becomes href="SubFolder/#"?

Tags:

asp.net

This isn't exactly Fermat's last theorem, but it keeps coming back to annoy me like an unpaid phone bill from college. Sometimes I want to create a HyperLink that does not cause a postback, so I want the target url to be #. When the markup happens to be from a UserControl in a subfolder,

/
|- Home.aspx (uses UC.ascx)
|- Sub
   |- UC.ascx

the URL is rewritten with a relative path, e.g.

<asp:HyperLink runat="server" NavigateUrl="#" >Click Me!</asp:HyperLink>

becomes

<a href="SubFolder/#">Click Me!</a>

Which is, unfortunately, wrong. Obviously I can get around this by not using a server control, but it seems stupid. Can this be avoided?

The point here is I will add a click event with jQuery or in code-behind, and I never want it to cause a postback, but I want it to be a hyperlink for CSS reasons.

like image 706
Jamie Treworgy Avatar asked Jan 06 '11 20:01

Jamie Treworgy


People also ask

What is NavigateUrl?

NavigateUrl is the property of Hyperlink where URL is defined and navigates to a given link.

Which control navigates to the page specified in its NavigateUrl property?

Use the NavigateUrl property to specify the URL to navigate to when the HyperLink control is clicked.


1 Answers

easy way:

 <asp:HyperLink ID="HyperLink1" 
                navigateUrl="#" 
                onclick="javascript:return false;"                   
                runat="server">HyperLink</asp:HyperLink>

or

 <asp:HyperLink ID="HyperLink1" 
                href="#" 
                runat="server">HyperLink</asp:HyperLink>

or jquery add a class to the link you don't want to have a postback (nopostback) :

$("a.nopostback").bind('click', function () {
     return false;
})
like image 103
Caspar Kleijne Avatar answered Nov 14 '22 06:11

Caspar Kleijne