Useful attributes for working with ObjectDataSource

1. DataObjectAttribute

Use the DataObjectAttribute attribute to identify an object as suitable for use by an ObjectDataSource object. Design-time classes such as the ObjectDataSourceDesigner class use the DataObjectAttribute attribute to present suitable objects to bind to an ObjectDataSource object.

2. DataObjectMethodAttribute

You can use the DataObjectMethodAttribute to identify data operation methods on a type marked with the DataObjectAttribute attribute so that they are more easily identified by callers using reflection. When the DataObjectMethodAttribute attribute is applied to a method, it describes the type of operation the method performs and indicates whether the method is the default data operation method of a type

Example:

[DataObjectAttribute]

public class NorthwindData

  public NorthwindData() {}

  [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
  public static IEnumerable GetAllEmployees()
  {
       AccessDataSource ads = new AccessDataSource();
      ads.DataSourceMode = SqlDataSourceMode.DataReader;
      ads.DataFile = "~//App_Data//Northwind.mdb";
      ads.SelectCommand = "SELECT EmployeeID,FirstName,LastName FROM Employees";
      return ads.Select(DataSourceSelectArguments.Empty);
  }

  [DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]
  public void DeleteEmployeeByID(int employeeID)
  {
       throw new Exception("The value passed to the delete method is " + employeeID.ToString());
  }
}

 

23 Comments

Comments have been disabled for this content.