ASP.NET - SQLDataSource - FilterExpression - 空字符串问题

发布于 2024-08-18 05:47:01 字数 1839 浏览 2 评论 0原文

在此测试应用程序中,我通过两个 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 技术交流群。

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

发布评论

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

评论(1

羁〃客ぐ 2024-08-25 05:47:01

尝试将两个 ConvertEmptyStringToNull 属性设置为 false。默认情况下它们是true,我猜空值会导致问题发生。

Try setting the ConvertEmptyStringToNull property of the two <asp:ControlParameter>'s to false. They're true by default and I'm guessing the nulls are throwing things off.

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