다음을 통해 공유


ObjectDataSource.FilterParameters 속성

정의

문자열의 모든 매개 변수 자리 표시자와 FilterExpression 연결된 매개 변수 컬렉션을 가져옵니다.

public:
 property System::Web::UI::WebControls::ParameterCollection ^ FilterParameters { System::Web::UI::WebControls::ParameterCollection ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public System.Web.UI.WebControls.ParameterCollection FilterParameters { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.FilterParameters : System.Web.UI.WebControls.ParameterCollection
Public ReadOnly Property FilterParameters As ParameterCollection

속성 값

ParameterCollection 속성에 있는 매개 변수 자리 표시자와 연결된 매개 변수 집합을 FilterExpression 포함하는 A입니다.

특성

예외

속성을 설정 FilterExpression 하면 메서드가 Select() a DataSet 또는 DataTable.를 반환하지 않습니다.

예제

이 섹션에는 두 가지 코드 예제가 포함되어 있습니다. 첫 번째 코드 예제에서는 중간 계층 비즈니스 개체에서 데이터를 검색 하는 개체를 사용 하 여 ObjectDataSource 필터링 된 데이터를 표시 하 고 결과 표시 하는 컨트롤을 GridView 보여 줍니다. 두 번째 코드 예제에서는 첫 번째 코드 예제에서 사용되는 중간 계층 비즈니스 개체의 예를 제공합니다.

다음 코드 예제에서는 중간 계층 비즈니스 개체에서 데이터를 검색 하는 컨트롤 및 GridView 결과 표시 하는 컨트롤을 사용 하 여 ObjectDataSource 필터링 된 데이터를 표시 하는 방법을 보여 줍니다. 컨트롤은 ObjectDataSource 데이터를 검색하는 메서드가 데이터를 개체 DataTableDataSet 검색할 때만 데이터를 필터링할 수 있습니다. 이러한 이유로 속성은 SelectMethod 데이터를 검색하는 비즈니스 개체 메서드를 DataSet식별합니다.

코드 예제는 컨트롤, 컨트롤, GridViewObjectDataSource 컨트롤 및 제출 단추로 구성됩니다TextBox. 기본적으로 Northwind TextBox Traders 직원 중 하나의 이름으로 채워집니다. 에 GridView 있는 이름으로 TextBox식별되는 직원에 대한 정보가 표시됩니다. 다른 직원의 데이터를 검색하려면 직원의 TextBox전체 이름을 입력한 다음 제출 단추를 클릭합니다.

이 속성은 FilterExpression 속성에서 검색 SelectMethod 되는 데이터를 필터링하는 데 사용되는 식을 지정합니다. 컬렉션에 포함된 FilterParameters 매개 변수로 평가되는 매개 변수 자리 표시자를 사용합니다. 이 예제에서는 매개 변수의 형식이 공백을 포함할 수 있는 문자열 형식이므로 매개 변수 자리 표시자는 작은따옴표로 제한됩니다. 매개 변수의 형식이 숫자 또는 날짜 형식인 경우 경계 따옴표가 필요하지 않습니다. 컬렉션에는 FilterParameters 컨트롤에 바인딩된 개체인 FormParameter 하나의 매개 변수가 TextBox 포함됩니다.

중요합니다

클라이언트에서 수신하는 필터 매개 변수 값의 유효성을 검사해야 합니다. 런타임은 매개 변수 값을 필터 식으로 대체하고 메서드에서 반환하는 개체에 Select 적용합니다DataView. 반환되는 항목 수를 제한하는 보안 조치로 속성을 사용하는 FilterExpression 경우 필터링이 발생하기 전에 매개 변수 값의 유효성을 검사해야 합니다.

<%@ 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 데이터 계층에서 데이터를 검색하는 대신 정적 데이터 집합을 만듭니다. 샘플이 필터링을 시연하기 위해 Northwind Traders 직원의 전체 이름을 제공하는 데 의존하기 때문에 이 예제에도 유용합니다. 전체 작업 예제의 경우 제공된 Web Forms 코드 예제와 함께 이러한 클래스를 컴파일하고 사용해야 합니다.

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

설명

컨트롤은 ObjectDataSource 메서드가 a 또는 DataTable 개체를 반환하는 Select 경우에만 데이터 필터링을 DataSet 지원합니다.

이 구문에 사용되는 FilterExpression 구문은 형식 문자열 스타일 식입니다. 필터 식 구문은 속성에서 허용하는 Expression 것과 동일한 구문입니다. 컬렉션에 매개 변수를 FilterParameters 추가하는 경우 형식 문자열 자리 표시자를 포함할 수도 있습니다. 예를 들어 매개 변수 값을 대체할 식에 포함합니다 "{0}" . 자리 표시자는 컬렉션에 있는 매개 변수의 인덱스로 FilterParameters 대체됩니다.

속성에 매개 변수를 포함할 FilterExpression 수 있습니다. 매개 변수가 문자열 또는 문자 형식인 경우 매개 변수를 작은따옴표로 묶습니다. 매개 변수가 숫자 형식인 경우에는 따옴표가 필요하지 않습니다.

이 속성은 FilterParameters 컨트롤과 연결된 개체에 ObjectDataSourceView 포함된 속성을 검색 FilterParameters 합니다ObjectDataSource.

적용 대상

추가 정보