如何在 Eval 格式字符串中使用单引号

发布于 2024-07-08 13:35:39 字数 1742 浏览 7 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尤怨 2024-07-15 13:35:39

不要忘记 .aspx 页面只是 XML。 您只需像平常一样转义引号即可。

例如:

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

当上述表达式进行数据绑定时,<%#%> 之间的值变为:

Eval("Id", "这是项目“{0}”。")

...当与具有“Id”属性值的对象数组进行数据绑定时,它会在 HTML 页面上生成输出1 至 5:

这是项目“1”。
这是项目“2”。
这是项目“3”。
这是项目“4”。
这是项目“5”。

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("Id", "This is item '{0}'.") %>" 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'.

很糊涂小朋友 2024-07-15 13:35:39

将 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.

冷月断魂刀 2024-07-15 13:35:39

为什么不在代码隐藏中将此 WHERE 子句定义为 const 呢? 定义:

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

那么您的 SelectCommand 属性将是:

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

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 ) %>'
寂寞清仓 2024-07-15 13:35:39

您是否尝试过转义单引号字符?

... WHERE (StringField = \'{0}\') ...

Have you tried escaping the single quote characters?

... WHERE (StringField = \'{0}\') ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文