简体   繁体   中英

javascript escape double quote in eval

I have a link button in gridview like below

<asp:LinkButton ID="lbtnEdit" runat="server" OnClientClick='<%# String.Format("Edit(\"{0}\", \"{1}\");return false;",Eval("Comment").ToString(),Eval("Status")) %>' >Edit</asp:LinkButton>

It works fine except there are double quote in comment. I tried change it to Eval("Comment").ToString().Replace(/'/g, "\\'") but got error like " The server tag is not well formed."

Any suggestion please

The Replace you tried is trying to use Javascript. Because the embedded quotes have already made it to the html page, Javascript no longer treats this as an entire string. Use the .NET version of Replace to encode the data server-side:

VB example:

Eval("Comment").ToString().Replace("""", "\""")

This will replace all quotes in the comment with escaped quotes, meaning they won't close or start a new string unexpectedly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM