Este artigo foi traduzido por máquina. Para visualizar o arquivo em inglês, marque a caixa de seleção Inglês. Você também pode exibir o texto Em inglês em uma janela pop-up, movendo o ponteiro do mouse sobre o texto.
Tradução
Inglês

ObjectDataSourceFilteringEventHandler Representante

Represents the method that will handle the Filtering event of the ObjectDataSource control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (em System.Web. dll)

public delegate void ObjectDataSourceFilteringEventHandler(
	Object sender,
	ObjectDataSourceFilteringEventArgs e
)

Parâmetros

sender
Tipo: System.Object

The source of the event, the ObjectDataSource.

e
Tipo: System.Web.UI.WebControls.ObjectDataSourceFilteringEventArgs

An ObjectDataSourceFilteringEventArgs that contains the event data.

When you create an ObjectDataSourceFilteringEventHandler delegate, you identify the method that will handle the event.Para associar o evento com o manipulador de eventos, adicione uma instância do delegate ao evento.O manipulador de evento é chamado sempre que o evento ocorre, a menos que você remova o delegate.For more information about event handler delegates, see Consumindo Eventos.

Esta seção contém dois exemplos de código.The first code example demonstrates how to display filtered data by using an ObjectDataSource control to retrieve data from a middle-tier business object, and then using a GridView control to display the results.O segundo exemplo de código fornece um exemplo de uma middle-tier business Objeto usado pelo Primeiro exemplo de código.

The following code example demonstrates how to display filtered data by using an ObjectDataSource control to retrieve data from a middle-tier business object, and then using a GridView control to display the results.The ObjectDataSource control can filter data only when the method that retrieves the data retrieves it as a DataSet or DataTable object.For this reason, the SelectMethod property identifies a business object method that retrieves data as a DataSet or DataTable object.

The code example consists of a TextBox control, a GridView control, the ObjectDataSource control, and a Submit button.By default, the TextBox control is populated with the name of one of the employees at Northwind Traders.The GridView control displays information about the employee who is identified by the name in the TextBox.To retrieve data for another employee, in the TextBox control, type the full name of the employee, and then click the Submit button.

The FilterExpression property specifies an expression that is used to filter the data that is retrieved by the method that is specified by the SelectMethod property.It uses parameter placeholders that are evaluated to the parameters that are contained in the FilterParameters collection.Neste exemplo, o espaço reservado de parâmetro está entre aspas simples (') porque o tipo do parâmetro é um tipo de Cadeia de Caracteres que pode conter espaços.Se o tipo do parâmetro for numérico ou data, as aspas não são necessárias.The FilterParameters collection contains one parameter, a FormParameter object that is bound to the TextBox control.

If no name is specified in the TextBox control, a new parameter is added to the ParameterValues collection so that the search is successful.

<%@ Register TagPrefix= Namespace= Assembly= %>
<%@ Page language= %>
<!DOCTYPE html PUBLIC "-
    "http:
<script runat=>

      ObjectDataSource1_Filtering(object sender, ObjectDataSourceFilteringEventArgs e)
    {
         (Textbox1.Text == )
        {
            e.ParameterValues.Clear();
            e.ParameterValues.Add(, );
        }
    }
</script>

<html xmlns="http:
  <head>
    <title>ObjectDataSource - C# Example</title>
  </head>
  <body>
    <form id= method= runat=>

        <p>Show all users with the following name.</p>

        <asp:textbox id= runat= text= />

        <asp:gridview
          id=
          runat=
          datasourceid=
          autogeneratecolumns=>
          <columns>
            <asp:boundfield headertext= datafield= />
            <asp:boundfield headertext= datafield= />
            <asp:boundfield headertext= datafield= />
          </columns>
        </asp:gridview>

        <!-- Security Note: The ObjectDataSource uses a FormParameter,
             Security Note: which does not perform validation of input  the client. -->

        <asp:objectdatasource
          id=
          runat=
          selectmethod=
          typename=
          filterexpression= OnFiltering=>
            <filterparameters>
              <asp:formparameter name= formfield= defaultvalue= />
            </filterparameters>
        </asp:objectdatasource>

        <p><asp:button id= runat= text= /></p>

    </form>
  </body>
</html>


<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.JSL" Assembly="Samples.AspNet.JSL" %>
<%@ Page language="VJ#" %>
<!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.get_Text() == "")
        {
            e.get_ParameterValues().Clear();
            e.get_ParameterValues().Add("FullName", "Nancy Davolio");
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ObjectDataSource - VJ# 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>

        <asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          selectmethod="GetAllEmployeesAsDataSet"
          typename="Samples.AspNet.JSL.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>


O seguinte exemplo de código fornece um exemplo de uma Meio-objeto corporativo camada que o acima usa exemplo de código.O exemplo de código é composto de duas classes básicas: EmployeeLogic and NorthwindEmployee.The EmployeeLogic class encapsulates business logic and the NorthwindEmployee class is a model class that contains only the basic functionality that is required to load and persist data from the data tier.For simplicity, the EmployeeLogic class creates a static set of data, rather than retrieving the data from a data tier.Para obter um exemplo de trabalho completa, você deve compilar e usar essas classes com exemplos de código de formulários da Web que são fornecidos.

 Samples.AspNet.CS {

 System;
 System.Collections;
 System.Data;
 System.Web.UI.WebControls;
  
  
  
  
    EmployeeLogic {


    
      ICollection GetAllEmployees () {
      ArrayList data =  ArrayList();

      data.Add( NorthwindEmployee(1,,,));
      data.Add( NorthwindEmployee(2,,,));
      data.Add( NorthwindEmployee(3,,,));
      data.Add( NorthwindEmployee(4,,,));
      data.Add( NorthwindEmployee(5,,,));
      data.Add( NorthwindEmployee(6,,,));
      data.Add( NorthwindEmployee(7,,,));

       data;
    }

      NorthwindEmployee GetEmployee(object anID) {
      ArrayList data = GetAllEmployees()  ArrayList;     
       empID = Int32.Parse(anID.ToString());      
       data[empID]  NorthwindEmployee;
    }

    
    
    
    
      DataSet GetAllEmployeesAsDataSet () {
      ICollection employees = GetAllEmployees();

      DataSet ds =  DataSet();

      
      DataTable dt =  DataTable();
      DataColumn dc;
      dc =  DataColumn(,   ());    dt.Columns.Add(dc);
      dc =  DataColumn(,(string)); dt.Columns.Add(dc);
      dc =  DataColumn(, (string)); dt.Columns.Add(dc);

      
      DataRow row;

       (NorthwindEmployee ne  employees) {                
        row = dt.NewRow();
        row[]    = ne.EmpID;
        row[] = ne.FullName;
        row[]  = ne.Address;
        dt.Rows.Add(row);
      } 
      
      ds.Tables.Add(dt);

       ds;
    }    
  }

    NorthwindEmployee {

     NorthwindEmployee ( anID, 
                              string aFirstName,
                              string aLastName,
                              string anAddress) {
      ID = anID;
      firstName = aFirstName;
      lastName = aLastName;   
      address = anAddress;
    }

     object ID;
     string EmpID {
       {  ID.ToString();  }
    }

     string lastName;
     string LastName {
       {  lastName; }
       { lastName = value; }
    }

     string firstName;
     string FirstName {
       {  firstName; }
       { firstName = value;  }
    }

     string FullName {
       {  FirstName  +  +  LastName; }
    }

     string address;
     string Address {
       {  address; }
       { address = value;  }
    }    

  }
}


package Samples.AspNet.JSL ; 
import System .* ;
import System.Collections .* ;
import System.Data .* ;
import 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;
    } //GetAllEmployees


    public static NorthwindEmployee GetEmployee(Object anID)
    {
        ArrayList data = (ArrayList)GetAllEmployees();
        int empID = Int32.Parse(String.valueOf(anID));

        return (NorthwindEmployee)data.get_Item(empID);
    } //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 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", int.class.ToType());
        dt.get_Columns().Add(dc);
        dc = new DataColumn("FullName", String.class.ToType());
        dt.get_Columns().Add(dc);
        dc = new DataColumn("Address", String.class.ToType());
        dt.get_Columns().Add(dc);

        // Add rows to the DataTable.
        DataRow row;

		IEnumerator iterator = employees.GetEnumerator();
        for (int iCtr = 0; iCtr < employees.get_Count(); iCtr++) {
			iterator.MoveNext();
            NorthwindEmployee ne = 
                (NorthwindEmployee)iterator.get_Current();

            row = dt.NewRow();
            row.set_Item("EmpID", ne.get_EmpID());
            row.set_Item("FullName", ne.get_FullName());
            row.set_Item("Address", ne.get_Address());
            dt.get_Rows().Add(row);
        }

        // Add the complete DataTable to the DataSet.
        ds.get_Tables().Add(dt);
        return ds;
    } //GetAllEmployeesAsDataSet
} //EmployeeLogic



public class NorthwindEmployee
{
    public NorthwindEmployee(int anID, String aFirstName, String aLastName, 
        String anAddress)
    {
        ID = new Integer(anID);
        firstName = aFirstName;
        lastName = aLastName;
        address = anAddress;
    } //NorthwindEmployee


    private Object ID;


    /** @property */
    public String get_EmpID()
    {
        return ID.toString();
    } //get_EmpID


    private String lastName;


    /** @property */
    public String get_LastName()
    {
        return lastName;
    } //get_LastName


    /** @property */
    public void set_LastName(String value)
    {
        lastName = value;
    } //set_LastName


    private String firstName;


    /** @property */
    public String get_FirstName()
    {
        return firstName;
    } //get_FirstName


    /** @property */
    public void set_FirstName(String value)
    {
        firstName = value;
    } //set_FirstName


    /** @property */
    public String get_FullName()
    {
        return firstName + " " + lastName;
    } //get_FullName


    private String address;


    /** @property */
    public String get_Address()
    {
        return address;
    } //get_Address


    /** @property */
    public void set_Address(String value)
    {
        address = value;
    } //set_Address
} //NorthwindEmployee


Contribuições da comunidade

ADICIONAR
Mostrar: