ASP.NET - SQLDataSource - FilterExpression - 空字符串问题
在此测试应用程序中,我通过两个 ControlParameters 进行过滤。正如您在第一个代码块中看到的,两个参数的默认值都是 %。这段代码效果很好。但是,请注意,在第二个代码块中,默认值已被消除。只要两个文本框的文本都不为空,第二个代码块就可以工作。由于某种原因,当 .NET 计算过滤器表达式并将“”插入到表达式中时,它似乎会出现错误并且不会过滤我的任何结果。
我可以通过为每个 ControlParameter 设置 defaultvalue="" 来验证此行为。
显然我已经找到了解决方法,但我想知道为什么 FilterExpression 在提供空字符串时会崩溃。
有什么想法吗?
代码块 1:
<asp:SqlDataSource ID="Customer_Data" runat="server"
ConnectionString='<%$ ConnectionStrings:connectionString %>'
SelectCommand="SELECT Station, StoreFront, CustomerID as CustID FROM vStations"
ProviderName="System.Data.SqlClient"
FilterExpression="StoreFront LIKE '%{0}%' and CustID LIKE '{1}%'"
EnableCaching="True"
CacheDuration="60">
<FilterParameters>
<asp:ControlParameter ControlID="TextBox1" PropertyName="Text" DefaultValue="%" />
<asp:ControlParameter ControlID="TextBox2" PropertyName="Text" DefaultValue="%" />
</FilterParameters>
</asp:SqlDataSource>
代码块 2:
<asp:SqlDataSource ID="Customer_Data" runat="server"
ConnectionString='<%$ ConnectionStrings:connectionString %>'
SelectCommand="SELECT Station, StoreFront, CustomerID as CustID FROM vStations"
ProviderName="System.Data.SqlClient"
FilterExpression="StoreFront LIKE '%{0}%' and CustID LIKE '{1}%'"
EnableCaching="True"
CacheDuration="60">
<FilterParameters>
<asp:ControlParameter ControlID="TextBox1" PropertyName="Text" />
<asp:ControlParameter ControlID="TextBox2" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
In this test application, I am filtering by two ControlParameters. As you can see in the first codeblock, both parameters have a default value of %. This code works great. However, in the second code block, please note that the Default Values are eliminated. The second code block works as long as neither Textbox's text is blank. For some reason, when .NET evaluates the filter expression and inserts "" into the expression, it seems to bug out and case none of my results to be filtered.
I am able to verify this behavior by setting the defaultvalue="" for each ControlParameter.
Obviously I have found a workaround, but I would like to know why the FilterExpression breaks down when an empty string is provided.
Any ideas?
Code Block 1:
<asp:SqlDataSource ID="Customer_Data" runat="server"
ConnectionString='<%$ ConnectionStrings:connectionString %>'
SelectCommand="SELECT Station, StoreFront, CustomerID as CustID FROM vStations"
ProviderName="System.Data.SqlClient"
FilterExpression="StoreFront LIKE '%{0}%' and CustID LIKE '{1}%'"
EnableCaching="True"
CacheDuration="60">
<FilterParameters>
<asp:ControlParameter ControlID="TextBox1" PropertyName="Text" DefaultValue="%" />
<asp:ControlParameter ControlID="TextBox2" PropertyName="Text" DefaultValue="%" />
</FilterParameters>
</asp:SqlDataSource>
Code Block 2:
<asp:SqlDataSource ID="Customer_Data" runat="server"
ConnectionString='<%$ ConnectionStrings:connectionString %>'
SelectCommand="SELECT Station, StoreFront, CustomerID as CustID FROM vStations"
ProviderName="System.Data.SqlClient"
FilterExpression="StoreFront LIKE '%{0}%' and CustID LIKE '{1}%'"
EnableCaching="True"
CacheDuration="60">
<FilterParameters>
<asp:ControlParameter ControlID="TextBox1" PropertyName="Text" />
<asp:ControlParameter ControlID="TextBox2" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将两个
的ConvertEmptyStringToNull
属性设置为 false。默认情况下它们是true
,我猜空值会导致问题发生。Try setting the
ConvertEmptyStringToNull
property of the two<asp:ControlParameter>
's to false. They'retrue
by default and I'm guessing the nulls are throwing things off.