Strongly Typed Data Controls Model Binding ASP.NET 4.5

Introduction

In previous versions of the ASP.NET framework, we have the ObjectDataSource control. But now in ASP.NET 4.5 Microsoft has integrated the ObjectDataSource control with Data Controls. Microsoft has exposed properties such as:

  1. SelectMethod
  2. UpdateMethod
  3. DeleteMethod

Purpose

Now the developer can easily bind data controls with the help of the SelectMethod and the developer can also do other Data Control operations with the help if UpdateMethod and DeleteMethod.

Code:

Step 1: First we will create a new Web Application in VS 2011.

Step 2: Now we'll create a public method which will return a list of objects.

public IQueryable<Customer> GetCustomers()
        {
           
IList<Customer> custlist = new List<Customer>();
            custlist.Add(
new Customer { ID = 1, Name = "Customer 1", Salary = 1000 });
            custlist.Add(
new Customer { ID = 2, Name = "Customer 2", Salary = 2000 });
            custlist.Add(
new Customer { ID = 3, Name = "Customer 3", Salary = 3000 });
            custlist.Add(
new Customer { ID = 4, Name = "Customer 4", Salary = 4000 });
            custlist.Add(
new Customer { ID = 5, Name = "Customer 5", Salary = 5000 });
            custlist.Add(
new Customer { ID = 6, Name = "Customer 6", Salary = 6000 });
            custlist.Add(
new Customer { ID = 7, Name = "Customer 7", Salary = 7000 });
            custlist.Add(
new Customer { ID = 8, Name = "Customer 8", Salary = 8000 });
           
return custlist.AsQueryable<Customer>();
        }
public class Customer   
{
       
public int ID { get; set; }
       
public string Name { get; set; }
       
public int Salary { get; set; }
}

Step 3: Now we'll add a data control to the aspx page. I have added a GridView Control:

  <asp:GridView ID="GridView1" SelectMethod="GetCustomers" AutoGenerateColumns="true"
       PageSize="3" AllowPaging="true" AllowSorting="true" runat="server">
  </asp:GridView>

Step 4: Now we'll run our application and see the output.

ASP.NET.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all