How to use Single Quotes in Eval Format String

喜你入骨 提交于 2019-11-28 13:35:13

Don't forget that a .aspx page is simply XML. You just escape the quotes as you normally would.

For example:

<asp:Repeater ID="repeatTheLabel" runat="server">
    <ItemTemplate>
        <asp:Label ID="Label1" Text="<%# Eval(&quot;Id&quot;, &quot;This is item '{0}'.&quot;) %>" runat="server" />
    </ItemTemplate>
    <SeparatorTemplate>
        <br />
    </SeparatorTemplate>
</asp:Repeater>

When the above expression is databound the value between <%# and %> becomes:

Eval("Id", "This is item '{0}'.")

...which produces on the HTML page as output when databound with an array of objects with "Id" property values from 1 to 5:

This is item '1'.
This is item '2'.
This is item '3'.
This is item '4'.
This is item '5'.

Store your sql queries in properties in your Page class. Not only does it work :-) but it makes your code easier to read and maintain.

Oh, and you should use parameters in your queries instead of doing string replacements. That will solve the problem by removing the need for single quotes.

Why don't you define this WHERE clause as a const in your codebehind. Define:

protected const string SELECTCLAUSE = 
"SELECT Blah FROM TableName WHERE (StringField = '{0}')";

Then your SelectCommand property would be:

SelectCommand='<%# Eval("Bar", SELECTCLAUSE ) %>'

Have you tried escaping the single quote characters?

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