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.
NavigateUrl is the property of Hyperlink where URL is defined and navigates to a given link.
Use the NavigateUrl property to specify the URL to navigate to when the HyperLink control is clicked.
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;
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With