Udostępnij za pośrednictwem


Binding to Business Objects

Business applications commonly employ an encapsulated data model using 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.

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

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

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

See Also

Other Resources

ObjectDataSource Web Server Control