简体   繁体   English

ASP.NET SqlDataSource筛选器行为

[英]ASP.NET SqlDataSource filter behaviour

I have a GridView object feed trough a SqlDataSource . 我有一个通过SqlDataSourceGridView对象供稿。 In the same form I have also a number of textBoxes used to build a filtering expression for the datasource. 同样,我也有许多用于为数据源构建过滤表达式的文本框。 The 'filtering' is started by pressing a button. 按下按钮开始“过滤”。

<asp:GridView
    DataSourceID="_sdsTable1"
    OnSorting="_gvTable1_Sorting"
    OnPageIndexChanging="_gvMovimenti_PageIndexChanging"
    runat="server"
    CssClass="list_table"
    ID="_gvTable1"
    CellPadding="0" CellSpacing="0"
    AutoGenerateColumns="false"
    EmptyDataText="No data."
    ShowHeader="true" ShowFooter="true"
    AllowSorting="true"
    AllowPaging="true"
    PageSize="10" 
    OnRowDataBound="_gvMovimenti_RowDataBound" >
    <HeaderStyle CssClass="header" />
    <FooterStyle CssClass="footer" />
    <PagerSettings
        Visible="true"
        Mode="NumericFirstLast"
        PageButtonCount="3"
        Position="Bottom"
        NextPageText="Next page"
        PreviousPageText="Prev page"
        FirstPageText="First page"
        LastPageText="Last page" />
    <RowStyle CssClass="odd" />
    <AlternatingRowStyle CssClass="even" />
    <PagerStyle HorizontalAlign="Center" />
    <Columns>
        <asp:TemplateField Visible="false">
         <HeaderTemplate>&nbsp;</HeaderTemplate>
         <ItemTemplate>
          <%#Eval("id")%>
         </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Date" SortExpression="date">
         <ItemTemplate>
          <%#Eval("date","{0:dd/MM/yyyy HH:mm:ss}")%>
         </ItemTemplate>
         <FooterTemplate>
          TOTALE:
         </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Price" SortExpression="price">
         <ItemTemplate>
          <asp:Label ID="_lblPrice" runat="server" Text='<%# Bind("price","{0:F2} &euro;") %>'>></asp:Label>
         </ItemTemplate>
         <FooterTemplate>
          <asp:Label ID="_lblTotal" runat="server" Text="0"></asp:Label
         </FooterTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="description" HeaderText="Description" SortExpression="description" />
    </Columns>
</asp:GridView>

<asp:SqlDataSource ID="_sdsTable1" runat="server"
    ConnectionString="<%$ ConnectionStrings:_db %>"
    ProviderName="<%$ ConnectionStrings:_db.ProviderName %>" 
    SelectCommand=" SELECT * FROM view1 WHERE id_user = @id_user;">
    <SelectParameters>
        <asp:SessionParameter Type="Int32" Name="id_user" SessionField="USER_ID" />
    </SelectParameters>
</asp:SqlDataSource>

In codebehind (in the event hanlder associated with the 'button' mentioned above) I build up the filter expression chaining the textbox values, with this code: 在后面的代码中(在与上述“按钮”相关联的hanlder事件中),我使用以下代码构建了链接文本框值的过滤器表达式:

    if (!string.IsNullOrWhiteSpace(_txtFilter0.Text.Trim()))
    {
        _sFilter += "(descrizione LIKE '%" + _txtFilter0.Text.Trim() + "%')";
    }

    if (!string.IsNullOrWhiteSpace(_txtFilter1.Text.Trim()))
    {
        if (!string.IsNullOrWhiteSpace(_sFilter))
            _sFilter += " AND";

        _sFilter += "(descrizione LIKE '%" + _txtFilter1.Text.Trim() + "%')";
    }

    _sdsTable1.FilterExpression = _sFilter;

Everything works until I clear the fields that leads to an empty filter. 一切正常,直到我清除导致空过滤器的字段。 I expected to retrieve all the data. 我希望检索所有数据。 For some reason, in this case, the last recordset is kept and shown apparently without a reason. 由于某种原因,在这种情况下,最后一个记录集显然没有理由被保留和显示。

I tried also to disable the SQLDataSource caching feature without luck: 我也尝试禁用SQLDataSource缓存功能,但是没有运气:

    EnableCaching="false"

I tried also to issue a Select command, again without luck: 我也尝试发出Select命令,再次没有运气:

 _sdsCrediti.Select(DataSourceSelectArguments.Empty);

Where I'm wrong? 我哪里错了?

Your _sFilter property will be persisted across postbacks, so as you're only updating it when your filter text boxes are not empty it will remain at the last set value when you clear them. _sFilter属性将在回发之间保持不变,因此,仅当过滤器文本框不为空时才更新它,因此清除它们时,它们将保留为最后的设置值。 Putting a breakpoint in your code at the line _sdsTable1.FilterExpression = _sFilter; 在代码中的_sdsTable1.FilterExpression = _sFilter;行中放置一个断点_sdsTable1.FilterExpression = _sFilter; should confirm this. 应该确认这一点。

To solve the issue, either clear the _sFilter property before rebuilding it in your event handler, or write an additional check: 要解决此问题,请先清除_sFilter属性,然后再在事件处理程序中对其进行重建,或者进行其他检查:

if (string.IsNullOrWhiteSpace(_txtFilter1.Text.Trim()) & string.IsNullOrWhiteSpace(_txtFilter0.Text.Trim()) )
{
  _sFilter = null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM