views:

500

answers:

3

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

+2  A: 

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.

slugster
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.
MarceloRamires
hah.. nevermind. i've noticed querrystrings work with <a>.. thank you!
MarceloRamires
A: 

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>
Dmytrii Nagirniak
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 ?
MarceloRamires
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
Dmytrii Nagirniak
A: 

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;
PhilPursglove