how to escape characters when using server side delimiters

為{幸葍}努か 提交于 2019-12-23 10:18:03

问题


So, currently, within an asp gridview, I have the following

<span id="btnEdit" runat="server" onclick="ShowEditCriteriaFilterDialog('<%#Eval("intSMCID")%>', '<%#Eval("strDescription")%>')" class="linkText">Edit</span>

What I'm essentially looking for is the syntax for quotes/double-quotes to actually accomplish this properly, as what I have above doesn't properly work.

First of all, if i encapsulate the entire onclick with single quotes, and not put any other quotes inside, it works for rendering purposes, but when i actually click the link at runtime, nothing happens.

If I encapsulate the entire onclick with double-quotes, like most properties of an ASPX element, it doesn't render properly, and everything after the comma which is after the first <%#Eval %> statement shows up as actual text on the screen. This leads me to believe there needs to be some escaping done to prevent it from thinking the click handler ends somewhere in the middle of that <%#Eval %> statement.

note that if I take away runat="server" and just encapsulate it in double-quotes, that seems to work better...but i need the span to be a server-side control for alot of other functionality I have in the page's code behind where I need to access the control through FindControl


回答1:


The Eval function needs double-quotes, so you have to wrap the attribute value in single quotes.

You can't mix static content and <%# ... %> blocks in a single property value for a control with runat="server", so you need to build the entire string within a single <%# ... %> block.

Within a string in a <%# ... %> block, you can use &apos; to insert a single quote:
EDIT That only works in .NET 4.0; in 2.0, it generates &amp;apos;, which won't work. Use double-quotes instead:

onclick='<%# string.Format(
   "ShowEditCriteriaFilterDialog(\"{0}\", \"{1}\")", 
   Eval("intSMCID"), Eval("strDescription")) %>'

Depending on your data, you might also need to encode the string value for JavaScript:

onclick='<%# string.Format(
   "ShowEditCriteriaFilterDialog(\"{0}\", \"{1}\")", 
   Eval("intSMCID"), 
   HttpUtility.JavaScriptStringEncode(Eval("strDescription", "{0}"))) %>'

(Code wrapped for readability - it needs to be on a single line.)




回答2:


I think the solution is

onclick='<%# "ShowEditCriteriaFilterDialog(" +Eval("intSMCID") + ","+ Eval("strDescription") +" );" %>'


来源:https://stackoverflow.com/questions/13863822/how-to-escape-characters-when-using-server-side-delimiters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!