28

I wanted to define a HyperLink control in asp.net that produces html output similar to the following:

<a href="#"></a>

How can this be done?

7 Answers 7

38

I agree with SLaks, but here you go

   <asp:HyperLink id="hyperlink1" 
                  NavigateUrl="#"
                  Text=""
                  runat="server"/> 

or you can alter the href using

hyperlink1.NavigateUrl = "#"; 
hyperlink1.Text = string.empty;
4
  • thanks I've tried this but here is the output: <a href="myPageUrl#"></a>, I don't need that myPageUrl chunk
    – mohas
    Mar 17, 2012 at 11:35
  • <a href="myPageUrl#"></a> and <a href="#"></a> both go to the same place...?... Are you sure, I just tried the source says <a id="hyperlink1" href="#"></a>
    – Ian G
    Mar 17, 2012 at 13:55
  • Slaks answer is OK, but I was looking to doing this with server side controls
    – mohas
    Oct 20, 2013 at 19:02
  • I realise this is old, but why would you want to use a server-side control for this? So you can add a tiny amount of extra server-side processing time for literally no gain? Jul 22, 2015 at 17:39
10

Just write <a href="#"></a>.

If that's what you want, you don't need a server-side control.

1
  • 1
    server side controls are useful in asp net events, like on databound of grids etc.
    – Ian Jowett
    Aug 16, 2017 at 8:37
3
hyperlink1.NavigateUrl = "#"; or
hyperlink1.attributes["href"] = "#"; or
<asp:HyperLink NavigateUrl="#" runat="server" />
1
  • 1
    It's ancient history, but: (1) it's .Attributes - capitalization. (2) setting the attribute is different from setting NavigateUrl, the latter expands to the url of the page the link is in with "#" on the end, the former gives you only "#".
    – philw
    Jan 15, 2016 at 17:26
1

If you want to add the value on aspx page , Just enter <a href='your link'>clickhere</a>

If you are trying to achieve it via Code-Behind., Make use of the Hyperlink control

HyperLink hl1 = new HyperLink();
hl1.text="Click Here";
hl1.NavigateUrl="http://www.stackoverflow.com";
0

Asp:Hyperlink http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlink.aspx

0

If you need to access this as a server-side control (e.g. you want to add data attributes to a link, as I did), then there is a way to do what you want; however, you don't use the Hyperlink or HtmlAnchor controls to do it. Create a literal control and then add in "Your Text" as the text for the literal control (or whatever else you need to do that way). It's hacky, but it works.

0

Solution

I solved this issue by typing

     <asp:HyperLink id="hyperlink1" href="#" Text="Some Text" runat="server"/> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.