0

I'm creating asp links using response.write in c#, the same HyperLink code works smoothly when inserted directly in the asp code, but when i copy/paste it to the response.write("...") it appears as an unclickable black text.

Am i forgetting something?

<asp:HyperLink ID='HyperLink1' runat='server' NavigateUrl='Exibe.aspx'> CLICK HERE </asp:HyperLink>

this exact code above thrown in the aspx source works greatly

response.write("<asp:HyperLink ID='HyperLink1' runat='server' NavigateUrl='Exibe.aspx'> CLICK HERE </asp:HyperLink>");

and this turns into a black text

1
  • another issue if you let me.. i have to figure a way to send a string in my code along in the querrystring of my <a> link.. and it wont let me concatenate the link code.. Commented Nov 30, 2009 at 12:06

3 Answers 3

2

You cannot insert an asp:Hyperlink tag directly into the response stream like that, as the hyperlink is actually a control that needs to "render" itself (if you replaced that with a normal "a" anchor/hyperlink tag it would work fine).

Instead you need to either create the control and add it to the page programatically, or maybe use a repeater control to render the anchors.

Sign up to request clarification or add additional context in comments.

2 Comments

my problem is.. this isn't the exact code, just a simpler one. i'm gonna use querrystrings, so the hyperlinks are gonna have to be created at runtime.
hah.. nevermind. i've noticed querrystrings work with <a>.. thank you!
0

You are trying to do totally different things:

  1. the markup (asp:HyperLink) will be compiled.
  2. the Response.Write("asp:HyperLink") will NOT. It will render text as is, and of course you wont't see any link, in fact you should see the text inside the tag asp:HyperLink (inluding the tag itself in the HTML source).

If you want to create a link dunamically you can do it using code snippets below:

<asp:HyperLink ID='HyperLink1' runat='server' NavigateUrl='<%= GetDynamicUrl() %>'> CLICK HERE </asp:HyperLink>
/// Or plain HTML
<a href="<%= GetDynamicUrl()"><%= GetTheLinkText() %></a>

2 Comments

well.. it has worked with normal <a> link.. but as i'm learning, i'm curious now.. this "GetDynamicUrl" runs in HTML.. i've never heard of it, where is it from ?
It is the public/protected method on the code-behind file (where you write your C# or VB.NET code). This method can return anything you want dynamically and is executed on the server
0

If you want to generate a hyperlink dynamically on the server-side like this, you can either use Response.Write with an <a> tag like slugster says, or alternatively consider the ASP:Literal control which renders exactly what you give it even if it contains markup e.g.

In your markup:

<asp:literal runat="server" id="MyLiteral" />

In your code:

string myHTMLFragment;

myHTMLFragment = "Hello. I am a link pointing to <a href="http:stackoverflow.com">StackOverflow</a>";

MyLiteral.Text = myHTMLFragment; 

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.