ASP.NET小技巧:使用Escape解決但双引号引发的问题。

一:先看个下面2个列子来引出问题

1.<input type="button"   onclick="javascript:popupWindowRefresh(510,'MessageBox.aspx?oid=<%#Eval("Id")%>&comment=<%#Eval("OrderComment")%>')"  value="留言" class="offWhiteBtn" />

2.<input type="button"  ID="MessageButton" runat="server" onclick="javascript:popupWindowRefresh(510,'MessageBox.aspx?oid=<%#Eval("Id")%>&comment=<%#Eval("OrderComment")%>')"
                                    value="留言" class="offWhiteBtn" />

第一个例子和第二个列子几乎相同唯一不同的是第二个是加了runat="server" 成为服务器标记。第一个例子可以编译通过,第二个无法编译同过,报什么“The server tag is not well formed.”的错误。

二:解决方案

不管是不是runat=server都是最外加单引号,里面用用双引号,如果里面还是要用字符串引号的话就用"\""可以解决。这个方案上面2种情况都可以用。

<input type="button"  ID="MessageButton" runat="server" onclick='<%#"javascript:popupWindowRefresh(500,"+"\""+string.Format("MessageBox.aspx?oid={0}&comment={1}",Eval("Id").ToString(),Eval("OrderComment").ToString())+"\""+")"%>'
value="留言" class="coffeeText" />

原文地址:https://www.cnblogs.com/scottpei/p/2382307.html