如何在 Eval 格式字符串中使用单引号
我有一个 Repeater 及其嵌套在 Gridview TemplatedField 内的 SqlDatasource。
Repeater 的数据源 SelectCommand 是使用 Gridview 中 Eval 的 FormatString 设置的。
SelectCommand 有一个 WHERE 子句,用于比较字符串。
因为我已经使用了单引号和双引号,所以在 SQL WHERE 子句中分隔字符串时遇到问题。
如何在 Eval FormatString 中添加单引号?
我尝试过使用 '替换'。
我尝试使用 '特殊字符' (... WHERE StringField = '{0}' 。 ..)
到目前为止还没有运气。 我感谢您能够提供的任何帮助。
<asp:GridView ID="GridView1" runat="server" DataSourceID="DataSource1" DataKeyNames="Foo" AutoGenerateColumns="False" AllowSorting="true" >
<Columns>
<asp:BoundField DataField="Foo" HeaderText="Foo" SortExpression="Foo" />
<asp:BoundField DataField="Bar" HeaderText="Bar" SortExpression="Bar" />
<asp:TemplateField>
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="DataSourceNested">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Blah") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="DataSourceNested" runat="server" DataFile="~/App_Data/DatabaseName"
SelectCommand='<%# Eval("Bar", "SELECT Blah FROM TableName WHERE (StringField = {0})") %>' >
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I've got a Repeater and its SqlDatasource nested inside a Gridview TemplatedField.
The Repeater's datasource SelectCommand is set using the FormatString of an Eval from the Gridview.
The SelectCommand has a WHERE clause which is to compare a string.
Because I have already used the single and double quotes, I am having trouble delimiting the string in the SQL WHERE clause.
How do I add single quotes inside an Eval FormatString?
I have tried using 'Replace'.
I have tried using 'Special Characters' (... WHERE StringField = '{0}' ...)
No luck so far. I appreciate any help you may be able to offer.
<asp:GridView ID="GridView1" runat="server" DataSourceID="DataSource1" DataKeyNames="Foo" AutoGenerateColumns="False" AllowSorting="true" >
<Columns>
<asp:BoundField DataField="Foo" HeaderText="Foo" SortExpression="Foo" />
<asp:BoundField DataField="Bar" HeaderText="Bar" SortExpression="Bar" />
<asp:TemplateField>
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="DataSourceNested">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Blah") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="DataSourceNested" runat="server" DataFile="~/App_Data/DatabaseName"
SelectCommand='<%# Eval("Bar", "SELECT Blah FROM TableName WHERE (StringField = {0})") %>' >
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要忘记 .aspx 页面只是 XML。 您只需像平常一样转义引号即可。
例如:
当上述表达式进行数据绑定时,
<%#
和%>
之间的值变为:...当与具有“Id”属性值的对象数组进行数据绑定时,它会在 HTML 页面上生成输出1 至 5:
Don't forget that a .aspx page is simply XML. You just escape the quotes as you normally would.
For example:
When the above expression is databound the value between
<%#
and%>
becomes:...which produces on the HTML page as output when databound with an array of objects with "Id" property values from 1 to 5:
将 SQL 查询存储在 Page 类的属性中。 它不仅有效:-)而且使您的代码更易于阅读和维护。
哦,您应该在查询中使用参数,而不是进行字符串替换。 这将通过消除单引号的需要来解决问题。
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.
为什么不在代码隐藏中将此 WHERE 子句定义为 const 呢? 定义:
那么您的 SelectCommand 属性将是:
Why don't you define this WHERE clause as a const in your codebehind. Define:
Then your SelectCommand property would be:
您是否尝试过转义单引号字符?
Have you tried escaping the single quote characters?