Y Chan
Y Chan

Reputation: 317

No double quote in hyperlink

I add the hyperlink in code behind in vb.net. I wanted to generate the link like this http://localhost:8010/CardExplorer.aspx?nc=2013+BC+100” target="_blank" >2013 BC 100 However, I used the following code didn't show the double quote. Please someone show me the correct code. thanks in advance.

Dim searchlink As New HyperLink
searchlink.Text = cn
searchlink.Attributes.Add("href", ConfigurationManager.AppSettings("Search") & Server.UrlEncode(cn) & """" & " target=" & """" & "_blank")
                        e.Item.Cells(6).Controls.Add(searchlink)

Also I used Chr(34) instead of using """" and the result is same as below.

<a href="http://localhost:8010/CardExplorer.aspx?nc=2013+BC+2566&quot; target=&quot;_blank">2013 BC 2566</a></td>

Upvotes: 0

Views: 309

Answers (2)

Gonzix
Gonzix

Reputation: 1206

Why don't you just use the hyperlink properties?

Dim searchlink As New HyperLink
searchlink.NavigateUrl = "http://foo.com"
searchlink.Target = "_blank"

Or even better, add the control on design time

<asp:HyperLink id="hyperlink1" ImageUrl="images/pict.jpg" NavigateUrl="http://www.microsoft.com" Text="Microsoft Official Site" Target="_blank" runat="server"/>       

Upvotes: 1

Adrian
Adrian

Reputation: 2875

You're using the one Attributes.Add() call to add all your attributes. Because of this it's assuming you want everything in the href attribute and is encoding quotes and similar characters appropriately to avoid generating invalid HTML. Try changing your code to look like this:

Dim searchlink As New HyperLink
searchlink.Text = cn
searchlink.Attributes.Add("href", ConfigurationManager.AppSettings("Search") & Server.UrlEncode(cn))
searchlink.Attributes.Add("target", "_blank")
e.Item.Cells(6).Controls.Add(searchlink)

Upvotes: 0

Related Questions