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. I now want to filter the results on specific columns (say column Customer), how would you go about this?
<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?
Hi You can use FilterExpression
for that purpose. 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
You can use the FilterParameters
property from markup and then specify one of the Parameter
sub classes. 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...
<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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.