다음을 통해 공유


Binding to Business Objects

Business applications commonly employ an encapsulated data model that uses a middle- tier data access layer or business component. Typically, in a Web application, you must write not only the code for the business data object, but additional code to communicate with the component, and more code to bind controls to the data managed by the component. The ASP.NET data source control model simplifies working with a business component and greatly reduces or eliminates the code you must write by providing the ObjectDataSource control, LINQ to SQL classes, and the LinqDataSource control.

The ObjectDataSource Control

The ObjectDataSource control works with any business object that encapsulates data and provides data-management services. The ObjectDataSource control uses the standard data source control object model, so that data-bound controls, such as the GridView control, can bind to it. The source business object can manage the data access directly or can serve as a wrapper for an existing business component. For more information, see Creating an ObjectDataSource Control Source Object.

You associate an ObjectDataSource control with a business object by setting the ObjectDataSource control's TypeName property to the type (class name) of the business object. You then specify one or more methods of the business object that the ObjectDataSource will call for specific data operations. The SelectMethod property of the ObjectDataSource control identifies the method of the source business object that will be used to retrieve data. Likewise, the InsertMethod, UpdateMethod, and DeleteMethod properties identify the methods that will be used for insert, update, and delete operations, respectively.

Once you have associated your ObjectDataSource control with the business object, you can bind a control to the ObjectDataSource control using the data-bound control's DataSourceID property, as shown in the following example.

<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ObjectDataSource - Visual Basic Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">

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

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

    </form>
  </body>
</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">
<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:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          selectmethod="GetAllEmployees"
          typename="Samples.AspNet.CS.EmployeeLogic" />

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

For more details, see the topics in the ObjectDataSource Web Server Control Overview section.

LINQ To SQL Classes

You can create classes that represent a database and its tables by using the Object Relational Designer. The classes that are generated by the designer use LINQ to SQL to perform data retrieval and modifications. After you create the classes, you can bind to the data objects by using the LinqDataSource control or the ObjectDataSource control. For more information about the LinqDataSource control, see LinqDataSource Web Server Control Overview. For more information about LINQ and ASP.NET, see Using LINQ with ASP.NET.

See Also

Concepts

LinqDataSource Web Server Control Overview

Reference

ObjectDataSource Web Server Control Overview