javascript - What are the possible levels of nested quotes in C#? -
i trying directly write javascript variable assignment code on asp.net web page.
response.write("<script>ithtml = '"); response.write("<div id=\"pop_ctrl\">select</div><ul id=\"demo_ul\">"); foreach (article in arts) { response.write("<li class=\"demo_li\"><a onclick=\"showarticlecard(" + a.id + ",\'" + a.user + "\',\'" + a.datestring + "\'); return false\"><div>" + it.user + "</div> <div>" + it.title + "</div></a></li>"); } response.write("</ul>"); response.write("';</script>"); anchor tag in markup executes function showarticlecard() on click. function accepts 1 int , 2 string parameters. when trying add c# string variable in place of string parameters, replaces them javascript keyword. tried using ',\' , \", error persists. so, there levels of nested quotes can use? if not how can resolve this?
try wrapping actual value (parameter) in quotes, this,
onclick=\"showarticlecard(" + a.id + ",'" + a.user + "','" + a.datestring + "'); // remaining code changed
i have removed single quotes int type param, , removed escape slash string types. can put as, '. when code run, considered string. otherwise (if value numeric) ignore these quotes , enter them are. string-type data requires wrapped in either ' or ". in javascript same. in c#, ' , " have different meanings, you know well.
tip: also, if writing client-side rendering, in asp.net, can write as,
string.format("<a href='{0}' onclick='func({1})'>my link</a>", hyperlink, param); this rendered want be. :) single quotes converted double quotes once rendered in browser. or can use @ before string, , write " inside string without having escape them.
Comments
Post a Comment