Stringbuilder problem creating dynamic links
-
22-09-2019 - |
Question
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&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!
Solution
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
(source: angelfire.com)
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.
OTHER TIPS
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 "".
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>"
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.
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);