ObjectDataSourceFilteringEventArgs 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为 Filtering 控件的 ObjectDataSource 事件提供数据。
public ref class ObjectDataSourceFilteringEventArgs : System::ComponentModel::CancelEventArgs
public class ObjectDataSourceFilteringEventArgs : System.ComponentModel.CancelEventArgs
type ObjectDataSourceFilteringEventArgs = class
inherit CancelEventArgs
Public Class ObjectDataSourceFilteringEventArgs
Inherits CancelEventArgs
- 继承
示例
本部分包含两个代码示例。 第一个 ObjectDataSource 代码示例演示如何通过使用控件从中间层业务对象检索数据,然后使用 GridView 控件显示结果来显示筛选的数据。 第二个代码示例提供了第一个代码示例使用的中间层业务对象的示例。
下面的代码示例演示如何通过使用控件 ObjectDataSource 从中间层业务对象检索数据,然后使用 GridView 控件显示结果来显示筛选的数据。 ObjectDataSource仅当检索数据的方法以 或 DataTable 对象的形式DataSet检索数据时,控件才能筛选数据。 因此, SelectMethod 属性标识一个业务对象方法,该方法以 或 DataTable 对象的形式DataSet检索数据。
代码示例由控件TextBox、控件、GridViewObjectDataSource控件和“提交”按钮组成。 默认情况下,控件 TextBox 填充为 Northwind Traders 中一名员工的姓名。 控件 GridView 显示有关员工的信息,该员工由 中 TextBox的姓名标识。 若要检索另一名员工的数据,请在 控件中 TextBox 键入该员工的全名,然后单击“ 提交 ”按钮。
属性 FilterExpression 指定一个表达式,该表达式用于筛选由 属性指定的 SelectMethod 方法检索的数据。 它使用参数占位符,这些占位符的计算结果为集合中包含的 FilterParameters 参数。 在此示例中,参数占位符用单引号括起来 (') ,因为参数的类型是可能包含空格的字符串类型。 如果参数的类型是数值或日期,则不需要引号。 集合 FilterParameters 包含一个参数,即 FormParameter 绑定到 控件的对象 TextBox 。
如果未在 控件中 TextBox 指定名称,则会将新参数添加到 ParameterValues 集合中,以便搜索成功。
重要
此示例包含一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述。
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS" Assembly="Samples.AspNet.CS" %>
<%@ Page language="c#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void ObjectDataSource1_Filtering(object sender, ObjectDataSourceFilteringEventArgs e)
{
if (Textbox1.Text == "")
{
e.ParameterValues.Clear();
e.ParameterValues.Add("FullName", "Nancy Davolio");
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ObjectDataSource - C# Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<p>Show all users with the following name.</p>
<asp:textbox id="Textbox1" runat="server" text="Nancy Davolio" />
<asp:gridview
id="GridView1"
runat="server"
datasourceid="ObjectDataSource1"
autogeneratecolumns="False">
<columns>
<asp:boundfield headertext="ID" datafield="EmpID" />
<asp:boundfield headertext="Name" datafield="FullName" />
<asp:boundfield headertext="Street Address" datafield="Address" />
</columns>
</asp:gridview>
<!-- Security Note: The ObjectDataSource uses a FormParameter,
Security Note: which does not perform validation of input from the client. -->
<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>
<p><asp:button id="Button1" runat="server" text="Search" /></p>
</form>
</body>
</html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB" Assembly="Samples.AspNet.VB" %>
<%@ Page language="vb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub ObjectDataSource1_Filtering(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceFilteringEventArgs)
If Textbox1.Text = "" Then
e.ParameterValues.Clear()
e.ParameterValues.Add("FullName", "Nancy Davolio")
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ObjectDataSource - VB Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<p>Show all users with the following name.</p>
<asp:textbox id="Textbox1" runat="server" text="Nancy Davolio" />
<asp:gridview
id="GridView1"
runat="server"
datasourceid="ObjectDataSource1"
autogeneratecolumns="False">
<columns>
<asp:boundfield headertext="ID" datafield="EmpID" />
<asp:boundfield headertext="Name" datafield="FullName" />
<asp:boundfield headertext="Street Address" datafield="Address" />
</columns>
</asp:gridview>
<!-- Security Note: The ObjectDataSource uses a FormParameter,
Security Note: which does not perform validation of input from the client. -->
<asp:objectdatasource
id="ObjectDataSource1"
runat="server"
selectmethod="GetAllEmployeesAsDataSet"
typename="Samples.AspNet.VB.EmployeeLogic"
filterexpression="FullName='{0}'" OnFiltering="ObjectDataSource1_Filtering">
<filterparameters>
<asp:formparameter name="FullName" formfield="Textbox1" defaultvalue="Nancy Davolio" />
</filterparameters>
</asp:objectdatasource>
<p><asp:button id="Button1" runat="server" text="Search" /></p>
</form>
</body>
</html>
下面的代码示例提供了上述代码示例使用的中间层业务对象的示例。 该代码示例由两个基本类组成: EmployeeLogic
和 NorthwindEmployee
。 类 EmployeeLogic
封装业务逻辑, NorthwindEmployee
类是一个模型类,仅包含从数据层加载和保存数据所需的基本功能。 为简单起见, EmployeeLogic
类创建一组静态数据,而不是从数据层检索数据。 对于完整的工作示例,必须编译这些类,并将其与提供的 Web 窗体代码示例一起使用。
namespace Samples.AspNet.CS {
using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;
//
// EmployeeLogic is a stateless business object that encapsulates
// the operations you can perform on a NorthwindEmployee object.
//
public class EmployeeLogic {
// Returns a collection of NorthwindEmployee objects.
public static ICollection GetAllEmployees () {
ArrayList data = new ArrayList();
data.Add(new NorthwindEmployee(1,"Nancy","Davolio","507 - 20th Ave. E. Apt. 2A"));
data.Add(new NorthwindEmployee(2,"Andrew","Fuller","908 W. Capital Way"));
data.Add(new NorthwindEmployee(3,"Janet","Leverling","722 Moss Bay Blvd."));
data.Add(new NorthwindEmployee(4,"Margaret","Peacock","4110 Old Redmond Rd."));
data.Add(new NorthwindEmployee(5,"Steven","Buchanan","14 Garrett Hill"));
data.Add(new NorthwindEmployee(6,"Michael","Suyama","Coventry House Miner Rd."));
data.Add(new NorthwindEmployee(7,"Robert","King","Edgeham Hollow Winchester Way"));
return data;
}
public static NorthwindEmployee GetEmployee(object anID) {
ArrayList data = GetAllEmployees() as ArrayList;
int empID = Int32.Parse(anID.ToString());
return data[empID] as NorthwindEmployee;
}
//
// To support basic filtering, the employees cannot
// be returned as an array of objects, rather as a
// DataSet of the raw data values.
public static DataSet GetAllEmployeesAsDataSet () {
ICollection employees = GetAllEmployees();
DataSet ds = new DataSet("Table");
// Create the schema of the DataTable.
DataTable dt = new DataTable();
DataColumn dc;
dc = new DataColumn("EmpID", typeof(int)); dt.Columns.Add(dc);
dc = new DataColumn("FullName",typeof(string)); dt.Columns.Add(dc);
dc = new DataColumn("Address", typeof(string)); dt.Columns.Add(dc);
// Add rows to the DataTable.
DataRow row;
foreach (NorthwindEmployee ne in employees) {
row = dt.NewRow();
row["EmpID"] = ne.EmpID;
row["FullName"] = ne.FullName;
row["Address"] = ne.Address;
dt.Rows.Add(row);
}
// Add the complete DataTable to the DataSet.
ds.Tables.Add(dt);
return ds;
}
}
public class NorthwindEmployee {
public NorthwindEmployee (int anID,
string aFirstName,
string aLastName,
string anAddress) {
ID = anID;
firstName = aFirstName;
lastName = aLastName;
address = anAddress;
}
private object ID;
public string EmpID {
get { return ID.ToString(); }
}
private string lastName;
public string LastName {
get { return lastName; }
set { lastName = value; }
}
private string firstName;
public string FirstName {
get { return firstName; }
set { firstName = value; }
}
public string FullName {
get { return FirstName + " " + LastName; }
}
private string address;
public string Address {
get { return address; }
set { address = value; }
}
}
}
Imports System.Collections
Imports System.Data
Imports System.Web.UI.WebControls
Namespace Samples.AspNet.VB
'
' EmployeeLogic is a stateless business object that encapsulates
' the operations you can perform on a NorthwindEmployee object.
'
Public Class EmployeeLogic
' Returns a collection of NorthwindEmployee objects.
Public Shared Function GetAllEmployees() As ICollection
Dim data As New ArrayList()
data.Add(New NorthwindEmployee(1, "Nancy", "Davolio", "507 - 20th Ave. E. Apt. 2A"))
data.Add(New NorthwindEmployee(2, "Andrew", "Fuller", "908 W. Capital Way"))
data.Add(New NorthwindEmployee(3, "Janet", "Leverling", "722 Moss Bay Blvd."))
data.Add(New NorthwindEmployee(4, "Margaret", "Peacock", "4110 Old Redmond Rd."))
data.Add(New NorthwindEmployee(5, "Steven", "Buchanan", "14 Garrett Hill"))
data.Add(New NorthwindEmployee(6, "Michael", "Suyama", "Coventry House Miner Rd."))
data.Add(New NorthwindEmployee(7, "Robert", "King", "Edgeham Hollow Winchester Way"))
Return data
End Function 'GetAllEmployees
Public Shared Function GetEmployee(anID As Object) As NorthwindEmployee
Dim data As ArrayList = CType(GetAllEmployees(), ArrayList)
Dim empID As Integer = Int32.Parse(anID.ToString())
Return CType(data(empID),NorthwindEmployee)
End Function 'GetEmployee
' To support basic filtering, the employees cannot
' be returned as an array of objects, rather as a
' DataSet of the raw data values.
Public Shared Function GetAllEmployeesAsDataSet() As DataSet
Dim employees As ICollection = GetAllEmployees()
Dim ds As New DataSet("Table")
' Create the schema of the DataTable.
Dim dt As New DataTable()
Dim dc As DataColumn
dc = New DataColumn("EmpID", GetType(Integer))
dt.Columns.Add(dc)
dc = New DataColumn("FullName", GetType(String))
dt.Columns.Add(dc)
dc = New DataColumn("Address", GetType(String))
dt.Columns.Add(dc)
' Add rows to the DataTable.
Dim row As DataRow
Dim ne As NorthwindEmployee
For Each ne In employees
row = dt.NewRow()
row("EmpID") = ne.EmpID
row("FullName") = ne.FullName
row("Address") = ne.Address
dt.Rows.Add(row)
Next
' Add the complete DataTable to the DataSet.
ds.Tables.Add(dt)
Return ds
End Function 'GetAllEmployeesAsDataSet
End Class
Public Class NorthwindEmployee
Public Sub New(anID As Integer, aFirstName As String, aLastName As String, anAddress As String)
ID = anID
Me.aFirstName = aFirstName
Me.aLastName = aLastName
Me.aAddress = anAddress
End Sub
Private ID As Object
Public ReadOnly Property EmpID() As String
Get
Return ID.ToString()
End Get
End Property
Private aLastName As String
Public Property LastName() As String
Get
Return aLastName
End Get
Set
aLastName = value
End Set
End Property
Private aFirstName As String
Public Property FirstName() As String
Get
Return aFirstName
End Get
Set
aFirstName = value
End Set
End Property
Public ReadOnly Property FullName() As String
Get
Return FirstName & " " & LastName
End Get
End Property
Private aAddress As String
Public Property Address() As String
Get
Return aAddress
End Get
Set
aAddress = value
End Set
End Property
End Class
End Namespace
注解
类 ObjectDataSourceFilteringEventArgs 用于将数据 Filtering 传递给 控件的 ObjectDataSource 事件处理程序。 属性 ParameterValues 提供对筛选参数值的访问权限,然后再执行筛选操作。 可以通过将 的 ObjectDataSourceFilteringEventArgs 属性设置为 Cancel 来true
取消 Select 方法。
构造函数
ObjectDataSourceFilteringEventArgs(IOrderedDictionary) |
使用指定的对象初始化 ObjectDataSourceFilteringEventArgs 类的新实例。 |
属性
Cancel |
获取或设置指示是否应取消事件的值。 (继承自 CancelEventArgs) |
ParameterValues |
获取一个 IOrderedDictionary 接口,该接口提供对 ObjectDataSource 类的 Parameter 对象的访问。 |
方法
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |