如何使用评估和演示格式字符串单引号 [英] How to use Single Quotes in Eval Format String

查看:132
本文介绍了如何使用评估和演示格式字符串单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个中继器和嵌套的GridView TemplatedField内其的SqlDataSource。

Repeater的数据源的SelectCommand使用在GridView的评估和演示的formatString的设置。

SelectCommand中有一个WHERE子句,这是比较字符串。

因为我已经使用了单引号和双引号,我有麻烦划定字符串中的SQL WHERE子句。

如何我一个eval的formatString内加单引号?

我一直在使用更换尝试。

我已经尝试使用特殊字符(... WHERE StringField =者; {0}者; ...)

至今没有运气。我AP preciate任何帮助,您可以提供。

 < ASP:GridView控件ID =GridView1=服务器的DataSourceID =DataSource1的DataKeyNames =富的AutoGenerateColumns =FALSEAllowSorting =真>
    <柱体和GT;
    < ASP:BoundField的数据字段=富的HeaderText =富SORTEX pression =富/>
    < ASP:BoundField的数据字段=酒吧的HeaderText =酒吧SORTEX pression =酒吧/>
    < ASP:的TemplateField>
    <&ItemTemplate中GT;
    < ASP:直放站ID =Repeater1=服务器的DataSourceID =DataSourceNested>
    <&ItemTemplate中GT;
    < ASP:标签ID =Label1的=服务器文本='<%#的eval(嗒嗒)%>'>< / ASP:标签>
    < / ItemTemplate中>
    < / ASP:直放站>
    < ASP:SqlDataSource的ID =DataSourceNested=服务器数据文件=〜/ App_Data文件/数据库名
                    的SelectCommand ='<%#的eval(酒吧,选择胡说FROM表名WHERE(StringField = {0}))%>' >
                < / ASP:SqlDataSource的>
    < / ItemTemplate中>
    < / ASP:的TemplateField>
    < /专栏>
< / ASP:GridView的>


解决方案

不要忘了,一个.aspx页面中简直是XML。你刚才逃脱的报价,你通常会。

例如:

 < ASP:直放站ID =repeatTheLabel=服务器>
    <&ItemTemplate中GT;
        < ASP:标签ID =Label1的文本=<%#评估和演示(安培; QUOT; ID和放大器; QUOT;,&安培; QUOT;这是项{0}'和; QUOT;)%>中=服务器/>
    < / ItemTemplate中>
    < SeparatorTemplate>
        < BR />
    < / SeparatorTemplate>
< / ASP:直放站>

在上述前pression一个数据绑定的值<%#%> 就变成了:


  

评估(ID,这是项{0}。)


...当它与带1ID属性值的对象数组到5数据绑定的HTML页面为输出的生产:


  

这是项'1'。

  这是项目2。

  这是项目'3'。

  这是项目'4'。

  这是项目5。


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>

解决方案

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

这篇关于如何使用评估和演示格式字符串单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆