Inside a string I have part of an html code with an onclick that calls a function. The problem is that I use double quotes inside double quotes that in turn have double quotes and when I pass it to the stringBuilder object to later build the page, the html code does not build it well for me.
The html code that goes inside the string would be this:
string codigo = "<div class='dropright'>" +
"<button type='button' onclick='__doPostBack(\"variableUno\",\"1\");'>" +
numeroBoton +
"</button>" +
"</div>"
So with StringBuilder I prepare the string to pass it to a json object format that will be an array of arrays. The code works fine for me if I remove the part of theonclick='__dopostback('variableUno','1')'
StringBuilder sb = new StringBuilder();
sb.Append("[[");
sb.Append("\"" + codigo + "\"");
sb.Append("]]");
Solution: In the original string, the slash must also be escaped
\
.Being that way:
The first two
\\
insert a slash and what follows\"
inserts the quote.Explanation:
The current output of
StringBuilder
is this:When you did the concatenation it replaced those
\"
in the original string with a quote, that's correct but it fails to convert to json because the inner quotes aren't being escaped.The output should be like this:
In order for the quotes in the final output to be escaped, the slash
\
before the quote must be preserved.