Software Technology Tips

 ObjectDataSource is one of the data source controls in ASP.NET.It allows us to bind DataBound Controls(GridView,DetailView,FormView etc). Most data source controls provide a two-tiered application architecture, where the presentation layer ie the User Interface interacts directly with the backend data provider.How ever to encapsulate data retrieval, an additional layer between the presentation page and data provider is required. The ObjectDataSource control allows developers to structure their applications as three-tiered architecture and is still developer friendly.

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.


Related Tags:

ASP.NET

Author: Manaswinee Mohapatra

ASP.NET

Let us Connect!

iso 9001 QA25 Red Herring STPI D&B Fastest Growing SME 2013 Award zinnov Nasscom

This site uses cookies. We respect your privacy.copyright (c) Mindfire Solutions 2007-2015. Login