简体   繁体   English

过滤ASP.NET ObjectDataSource?

[英]Filtering an ASP.NET ObjectDataSource?

I have a ObjectDataSource in a standard ASP.NET WebForm which allows effective paging by only loading the data required for the page the user is viewing. 我在标准的ASP.NET WebForm中有一个ObjectDataSource,它允许通过仅加载用户正在查看的页面所需的数据来进行有效的分页。 I now want to filter the results on specific columns (say column Customer), how would you go about this? 我现在想要过滤特定列上的结果(比如列Customer),你会怎么做?

<asp:ObjectDataSource ID="ticketsDataSource" runat="server"
        SelectMethod="GetTicketsData" EnablePaging="true"
        MaximumRowsParameterName="pageSize"
        StartRowIndexParameterName="startRowIndex"
        TypeName="SupportSystem.App_Code.TicketsDataSource" SelectCountMethod="TotalRowCount">
        <SelectParameters>
            <asp:Parameter Name="startRowIndex" Type="Int32" />
            <asp:Parameter Name="pageSize" Type="Int32" />
        </SelectParameters>
</asp:ObjectDataSource>

I was previously doing everything in code behind as follows, but this meant once numerous tickets were added, the grid was very slow when paging etc as it was loading all the data before rendering etc. 我之前在代码中执行的操作如下所示,但这意味着一旦添加了大量的票证,在分页等时网格非常慢,因为它在渲染之前加载了所有数据等。

DataSet ds = new DataSet();
Dictionary<string, object> d = new Dictionary<string, object>();
d.Add("@params", "");
ds.ReadXml(new System.IO.StringReader(Database.DAL_XML("Usp_Fetch_Tickets", d).OuterXml));
if (Database_error(ds))
{
   return;
}
var dv = ds.Tables[0].DefaultView;
dv.RowFilter = "Customer LIKE '%abc%'";
var newDS = new DataSet();
var newDT = dv.ToTable();
newDS.Tables.Add(newDT);
gvTickets.DataSource = newDS;
gvTickets.DataBind();

Any ideas of how to effectively filter the ObjectDataSource? 有关如何有效过滤ObjectDataSource的任何想法?

Hi You can use FilterExpression for that purpose. 您好您可以使用FilterExpression为此目的。 Following is the sample snippet how to achieve that. 以下是示例代码段如何实现。

<asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          selectmethod="GetAllEmployeesAsDataSet"
          typename="Samples.AspNet.CS.EmployeeLogic"
          filterexpression="FullName='{0}'" OnFiltering="ObjectDataSource1_Filtering">
            <filterparameters>
              <asp:formparameter name="FullName" formfield="Textbox1" defaultvalue="Nancy Davolio" />
            </filterparameters>
        </asp:objectdatasource>

Additional info can be found on the following msdn page check this link 更多信息可以在以下msdn页面上找到此链接

You can use the FilterParameters property from markup and then specify one of the Parameter sub classes. 您可以使用标记中的FilterParameters属性,然后指定其中一个Parameter子类。 For example, suppose you have a TextBox that you provide the user to input a customer name to filter the results by that customer, you will need to use a ControlParameter for this... 例如,假设您有一个TextBox ,您提供用户输入客户名称以过滤该客户的结果,您将需要使用ControlParameter ...

<asp:ObjectDataSource>
      <FilterParameters>
          <asp:ControlParameter ControlID="txtCustomer" PropertyName="Text" Name="customerName" />
      </FilterParameters>
</asp:ObjectDataSource>

here txtCustomer would be the ID of the TextBox , Text is the property of that control to get the input value from and customerName is a parameter in the GetTicketsData method that you need to expose in order to pass in the filter value 这里txtCustomer将是TextBox的ID, Text是该TextBox的属性,用于获取输入值,而customerNameGetTicketsData方法中的一个参数,您需要公开该参数以传递过滤器值

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

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