开发者

Creating clickable links in runtime of c# aspx webform

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 i开发者_如何学Pythonn 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


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.


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>


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜