views:

221

answers:

5

Hi all,

I'm having an issue creating a link like "<a href="javascript:window.open('www.microsoft.com');">Visit Microsoft</a> using stringbuilder. I am adding html to a panel dynamically and I am trying to create a popup link.

The problem is that for some reason the link gets "mixed up". For example:

Dim s As String
sb.Append("<A HREF='javascript:void(0)' onclick='window.open(")
sb.Append("'")
sb.Append("Match.aspx?MatchID=")
sb.Append(mt.MatchID)
sb.Append("&batchid=")
sb.Append(mb.batchID)
sb.Append("')>")
sb.Append("Match</A>")

gives an output of :

<a match.aspx?matchid="28840&amp;batchid=26596')" onclick="window.open(" href="javascript:void(0)">Match</a>

I have no clue what I'm doing wrong, it even does this kind of crap for a regular string!

Please help!

A: 

Why not try using this.

Dim s as string
s = "<A HREF='javascript:void(0)' onclick='window.open('Match.aspx?MatchID=" _
    & mt.MatchID & "&batchid=" & mb.batchID & "')Match</A>"
poh
+3  A: 

Your output isn't a valid HTML:

<A HREF='javascript:void(0)' onclick='window.open('Match.aspx?MatchID=10&batchid=10')>Match</A>

You need to output this HTML:

<A HREF="javascript:void(0)" onclick="window.open('Match.aspx?MatchID=10&batchid=10')">Match</A>

How about:

Dim s As String
sb.Append("<A HREF=""javascript:void(0)"" onclick=""window.open(")
sb.Append("'")
sb.Append("Match.aspx?MatchID=")
sb.Append(mt.MatchID)
sb.Append("&batchid=")
sb.Append(mb.batchID)
sb.Append("')>""")
sb.Append("Match</A>")

What you need to do is to make sure that the output is a valid HTML, and you don't mix the attributes quotes with the JavaScript string quotes.

Edit: Just noticed that this is VB, so the escaped character need to be "".

Mendy
A: 

Have you considered using string.Format?

stringToFormat.Format("<A HREF="javascript:void(0)" onclick="window.open('Match.aspx?MatchID={0}&batchid={1}')">Match</A>", mt.MatchID, mb.batchID);

Also helps you see the string clearly in examples like this and the mistakes in the HTML pop out at you. Fixed some quotes.

Kyle Rozendo
+1  A: 

Here is your code, plus a declaration of a Stringbuilder, that I pasted into LINQPad

Sub Main
Dim sb As New Stringbuilder
Dim s As String
sb.Append("<A HREF=""javascript:void(0)"" onclick=""window.open(")
sb.Append("'")
sb.Append("Match.aspx?MatchID=")
sb.Append("45") 'Used random numbers for MatchID
sb.Append("&batchid=")
sb.Append("45") 'Used random numbers for batchid
sb.Append("')")
sb.Append(""">")
sb.Append("Match</A>")
Console.WriteLine(sb)
End Sub

And here is what I got

Here is what I got.

Besides the output not being HTML format, I cannot see how we get different results.

EDIT:

I have changed the code around to produce HTML format along with an updated photo.

Anthony Forloney
Thank You!Thank You!Thank You!Thank You!Thank You!Thank You!Thank You!Thank You! a hundred times more!!!
wali
No problem, glad I could help.
Anthony Forloney
A: 

Using sb.Append like that is a bit long winded - an alternative is:

string myHTML = string.Format("<A HREF='javascript:void(0)' onclick='window.open(\"Match.aspx?MatchID={0}&batchid={1}\")'>Match</A>"
              ,mt.MatchID
              ,mb.batchID
             );

note the mixed use of single and double quotes in the onclick() function.

Of course maybe an even better way is to declare a new HTML element like this:

HtmlLiteral myAnchor = new HtmlLiteral("A");
myAnchor.Attributes.Add("href", "javascript:void(0);");
myAnchor.Attributes.Add("onclick", "my javascript");
...etc...
myPanel.Controls.Add(myAnchor);
slugster