Cool Data Binding Enhancements in ASP.NET 4.5

Data binding is a significant feature that is required by data driven ASP.NET applications. To make use of ASP.NET data controls, data binding is a must. In this article I will take you through the new data binding features that are introduced in ASP.NET Framework 4.5.  It will also include code samples to make the explanation easier to understand.

There are two important data binding enhancements and they are:

1. Strong typing of the ASP.NET data controls

2. Model binding in ASP.NET data controls

These features make the development and maintainability of the code a lot easier. It also adds a pinch of step up in the page performance.

Strong Typing of the ASP.NET Data Controls

In earlier versions of ASP.NET there was a way to bind the data to the child controls of an ASP.NET data control using the keyword Eval and mentioning the property name of the binding data model in double quotes as a string. ASP.NET used reflection at runtime in order to resolve the data type and bind the actual value of the model property to the control. In this approach there was no intellisense assistance for the developers and the code was difficult to maintain.

In ASP.NET 4.5 the data control can be strongly typed and there by Visual Studio will help with intellisense and inference of the data type at design time. The ASP.NET controls now have a property called ItemType to which the binding model type should be mentioned. Once that is done on the child controls once you type Item followed by a dot then Visual Studio IDE will present you with the intellisense displaying the properties of the binding model. If the binding has to be two way then replace Item with BindingItem.

Code Sample

In this example a list of Vehicle model is bound to a repeater and the repeater will highlight both one way and two way strongly typed binding.

Create a sample web application and add a Vehicle.cs class to it.

namespace WebApplication1.App_Code
{
    public class Vehicle
    {
        public int ID { get; set; }
        public string Color { get; set; }

 public string VehicleModel { get; set; }
        public string CommentFromMechanic { get; set; }
    }
}

Now add a web form and drag drop a repeater control. Fig 1.0 will show the intellisense of the binding model.

The intellisense of the binding model
Fig 1.0 The intellisense of the binding model

Here is the designer code.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="frpStrongControlTypingDemo" runat="server">
    <div>
        <asp:Repeater ID="rptVehicles" runat="server" ItemType="WebApplication1.App_Code.Vehicle">
            <ItemTemplate>
                <asp:Label ID="lblID" Text="<%# Item.ID %>" runat="server"></asp:Label>
                <asp:TextBox ID="txtComments" TextMode="MultiLine" Text="<%# BindItem.CommentFromMechanic %>" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

Model Binding in ASP.NET Data Controls

You provided the data access methods to an ObjectDataSource in the earlier versions of ASP.NET, the 4.5 version allows you to provide them directly onto the data controls. Below is the three data access methods supported.

1. SelectMethod

2. UpdateMethod

3. DeleteMethod

The select method should return an IEnumerable or IQueryable type. The select method also can be used to filter the selected data.

Code Sample

Create a sample web application and create a model class Employee.cs.

namespace WebApplication1.App_Code
{
    public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string JobTitle { get; set; }
    }
}

Add a web form and add the below mentioned code to it.

<form id="frmModelBindingDemoDemo" runat="server">
        <div>
            Name Contains:
            <asp:TextBox ID="txtNameFilter" runat="server"></asp:TextBox>
            <asp:Button ID="btnFilter" runat="server" Text="Filter" OnClick="btnFilter_Click" />
 
            <asp:GridView ID="gvEmployee" runat="server" ItemType="WebApplication1.App_Code.Employee" SelectMethod="GetEmployees" AutoGenerateColumns="false">
                <Columns>
                    <asp:TemplateField HeaderText="Name">
                        <ItemTemplate>
                            <asp:TextBox ID="txtName" runat="server" Text='<%# BindItem.Name %>'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="JobTitle">
                        <ItemTemplate>
                            <asp:TextBox ID="txtTitle" runat="server" Text='<%# BindItem.JobTitle %>'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
    </form>

In the code behind file of the .aspx page, add the method to select the data, which also performs the filter based on the value entered in the textbox.

        protected IQueryable<Employee> GetEmployees([System.Web.ModelBinding.Control("txtNameFilter")] string nameFilter)
        {
            List<Employee> employees = EmployeeDataHelper.GetAllEmployees();
            return employees.Where(employee => employee.JobTitle.Contains(nameFilter)).AsQueryable();
        }

Hope this article explained the new data binding improvements in ASP.NET 4.5. Please feel free to add in your feedback comments.

Happy reading!

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read