An ObjectDataSource control can be used to represent any type of Object in .Net framework.We can use ObjectDataSource control with components that represent Collections,ADO.Net Objects(DataReader,DataSet etc),LINQ to SQL queries and Web Services.
Properties:
ObjectDataSource Control has five mostly used properties:
1.TypeName :the name of the type of object the control representing
2.SelectMethod :the method name the ObjectDataSource calls for selecting data
3.UpdateMethod :the method name the ObjectDataSource calls for updating data
4.InsertMethod :the method name the ObjectDataSource calls for inserting data
5.DeleteMethod :the method name the ObjectDataSource calls for deleting data
Binding ObjectDataSourceControl to a collection:
For binding the ObjectDataSourceControl with a collection we have to use the TypeName and SelectMethod properties of the control .Lets start with a class called Employees which has a method called LoadEmployeesData() which returns a collection of Employees' names.Our class will be like the following one.
public class Employees
{
public List<string> LoadEmployeesData()
{
List<string> oEmployee = new List<string>();
oEmployee.Add("Manaswinee");
oEmployee.Add("Aryanwita");
oEmployee.Add("Anusikta");
oEmployee.Add("Anisha");
oEmployee.Add("Aloevera");
oEmployee.Add("Nilofer");
oEmployee.Add("Rainy");
oEmployee.Add("Techie");
return oEmployee;
}
}
and in our .aspx page ie the designer page will include a GridView control along with an ObjectDataSource control.The GridView control is bound to the ObjectDataSource control through its DataSourceID property.
The TypeName property of ObjectDataSource control will be the component which the control is going to represent that is "'Employees" where as the SelectMethod will be the method name which the ObjectDataSource control calls for selecting data.In our case it will be LoadEmployeesData.
<asp:GridView ID="GridViewEmp" runat="server"
DataSourceID="ObjectDataSourceEmp" Width="226px">
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSourceEmp" runat="server"
TypeName="Employees" SelectMethod="LoadEmployeesData" >
</asp:ObjectDataSource>
When we will open the page a list of Employee names will be displayed in the GridView.