다음을 통해 공유


ObjectDataSourceObjectEventHandler 대리자

정의

컨트롤의 이벤트와 ObjectCreated 처리 ObjectCreating 할 메서드를 ObjectDataSource 나타냅니다.

public delegate void ObjectDataSourceObjectEventHandler(System::Object ^ sender, ObjectDataSourceEventArgs ^ e);
public delegate void ObjectDataSourceObjectEventHandler(object sender, ObjectDataSourceEventArgs e);
type ObjectDataSourceObjectEventHandler = delegate of obj * ObjectDataSourceEventArgs -> unit
Public Delegate Sub ObjectDataSourceObjectEventHandler(sender As Object, e As ObjectDataSourceEventArgs)

매개 변수

sender
Object

이벤트의 출처입니다.

e
ObjectDataSourceEventArgs

ObjectDataSourceEventArgs 이벤트 데이터가 들어 있는 항목입니다.

예제

다음 코드 예제에서는 비즈니스 개체와 GridView 컨트롤을 사용 하 여 정보를 검색 하 고 표시 하는 컨트롤을 사용 ObjectDataSource 하는 방법을 보여 줍니다. 이 예제에서는 많은 실제 시나리오에서와 같이 비즈니스 개체의 기본 인스턴스를 컨트롤과 함께 ObjectDataSource 사용할 수 없거나 적절하지 않을 수 있습니다. 이 예제에서는 예외를 ObjectDataSource throw하기 때문에 매개 변수가 없는 생성자를 성공적으로 호출할 수 없습니다. 경우에 따라 매개 변수가 없는 생성자가 보호될 수 있으며, 다른 경우에는 비즈니스 개체를 원하는 상태로 초기화하지 않을 수 있습니다. 이유가 무엇이든 비즈니스 개체를 직접 인스턴스화하고 처리기에 ObjectInstance 전달되는 개체의 ObjectDataSourceEventArgs 속성으로 인스턴스를 설정할 수 있습니다. 이 인스턴스는 해당 작업을 수행하는 데 사용할 비즈니스 개체 인스턴스 ObjectDataSource 입니다.

<%@ Import namespace="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">
private void NorthwindLogicCreating(object sender, ObjectDataSourceEventArgs e)
{
    // Create an instance of the business object using a non-default constructor.
    EmployeeLogic eLogic = new EmployeeLogic("Not created by the default constructor!");
    
    // Set the ObjectInstance property so that the ObjectDataSource uses the created instance.
    e.ObjectInstance = eLogic;
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ObjectDataSource - C# Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">

        <asp:gridview
          id="GridView1"
          runat="server"
          datasourceid="ObjectDataSource1">
        </asp:gridview>

        <asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          selectmethod="GetAllEmployees"
          onobjectcreating="NorthwindLogicCreating"
          typename="Samples.AspNet.CS.EmployeeLogic" >
        </asp:objectdatasource>

    </form>
  </body>
</html>
<%@ Import namespace="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">
Private Sub NorthwindLogicCreating(sender As Object, e As ObjectDataSourceEventArgs)

    ' Create an instance of the business object using a non-default constructor.
    Dim eLogic As EmployeeLogic = New EmployeeLogic("Not created by the default constructor!")
    
    ' Set the ObjectInstance property so that the ObjectDataSource uses the created instance.
    e.ObjectInstance = eLogic
    
End Sub ' NorthwindLogicCreating

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ObjectDataSource - VB Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">

        <asp:gridview
          id="GridView1"
          runat="server"
          datasourceid="ObjectDataSource1">
        </asp:gridview>

        <asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          selectmethod="GetAllEmployees"
          onobjectcreating="NorthwindLogicCreating"
          typename="Samples.AspNet.VB.EmployeeLogic" >
        </asp:objectdatasource>

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

다음 코드 예제에서는 앞의 예제에서 사용 하는 예제 기본 비즈니스 개체를 보여 줍니다.

namespace Samples.AspNet.CS {

using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;

  public class EmployeeLogic {

    public EmployeeLogic() {  
        throw new NotSupportedException("Initialize data.");
    }
    
    public EmployeeLogic(string data) {
        _data = data;
    }

    private string _data;
    
    // Returns a collection of NorthwindEmployee objects.
    public ICollection GetAllEmployees () {
      ArrayList al = new ArrayList();      
      al.Add(_data);        
      return al;
    }
  }
}
Imports System.Collections
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Samples.AspNet.VB
  Public Class EmployeeLogic
    
    
    Public Sub New() 
        Throw New NotSupportedException("Initialize data.")
    
    End Sub
    
    
    Public Sub New(ByVal data As String) 
        _data = data
    
    End Sub
    
    Private _data As String
    
    
    ' Returns a collection of NorthwindEmployee objects.
    Public Function GetAllEmployees() As ICollection 
        Dim al As New ArrayList()
        al.Add(_data)
        Return al
    
    End Function 'GetAllEmployees
  End Class
End Namespace ' Samples.AspNet.VB

설명

대리자를 ObjectDataSourceObjectEventHandler 만들 때 이벤트를 처리할 메서드를 식별합니다. 이벤트를 이벤트 처리기와 연결하려면 대리자의 인스턴스를 이벤트에 추가합니다. 대리자를 제거하지 않는 한 이벤트가 발생할 때마다 이벤트 처리기가 호출됩니다. 이벤트를 처리하는 방법에 대한 자세한 내용은 이벤트 처리 및 발생을 참조하세요.

확장명 메서드

Name Description
GetMethodInfo(Delegate)

지정된 대리자가 나타내는 메서드를 나타내는 개체를 가져옵니다.

적용 대상

추가 정보