Srikanth Technologies

New Features of ASP.NET 4.5

ASP.NET 4.5 is part of Microsoft.Net 4.5. Since its predecessor, ASP.NET 4.0, the following features were added to ASP.NET 4.5.

Also note, I am writing about ASP.NET 4.5 Web Forms. As you may be aware of, ASP.NET has three flavors - ASP.NET Web Pages, ASP.NET Web Forms and ASP.NET MVC.

Support for Multiple Files Upload

Since HTML 5 provides support for multiple files upload from browsers, ASP.NET 4.5 has provided new methods and properties to support multiple files upload.

Following are the new properties of FileUpload control that support multiple files upload.

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

    protected void btnUpload_Click(object sender, EventArgs e)
    {
      // Check whether FileUpload has any files 
      if (FileUpload1.HasFiles)
      {
       // Iterate over files
       foreach (HttpPostedFile file in FileUpload1.PostedFiles)
       {
            file.SaveAs(Server.MapPath("~/files/") + file.FileName);
       }
       lblMsg.Text = "Uploaded " +  FileUpload1.PostedFiles.Count + " files!";
      }

    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server">
 Select Files to upload : 
 <asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true"/>
 <p />
 <asp:Button ID="btnUpload" runat="server" Text="Upload"  
    OnClick="btnUpload_Click" />
 <p />
 <asp:Label ID="lblMsg" runat="server" Text=""></asp:Label>
</form>
</body>
</html>

Strongly Typed Data Binding

In previous version of ASP.NET data binding expressions were not strongly typed. All that we had was eval() and bind() functions to bind data to controls.

But whether the expression given with eval() and bind() is valid or not is not known until runtime. So these expressions do not support IntelliSense for member names, and compile-time checking.

ASP.NET 4.5 introduced a new property called ItemType for data bind controls like DataList.  ItemType specifies the type of data that is being bound to control.

Inside the scope of data-binding expression, two new variables – Item and BindItem – are made available. These variables are strongly typed and support intellisense, compile-time checking etc.

The following example shows how to use strongly typed binding.

Create a class that is used in data binding.

public class Employee
{
    public int Id { get;set;}
    public string Name { get;set;}
    public string JobTitle { get;set;}
    public int Salary { get;set; }

}
Then create an Asp.net page with DataList and specify the type of the item that is bound to items in DataList is Employee using ItemType attribute of DataList. For simplicity sake, I created a List of Employee objects and bind the list to DataList.

Inside data-binding expressions of DataList, you can use Item variable to get access to Employee object. So, Item.Name refers to name of the employee, Item.Salary refers to salary of the employee. We get Intellisence support with Item variable.

<%@ Page Language="C#" %>

<!DOCTYPE html>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        List<Employee> employees = new List<Employee> {
              new Employee { Id = 1,  Name = "Ronaldo", JobTitle = "Programmer", Salary = 50000},
              new Employee { Id = 2,  Name = "Alanso", JobTitle = "Tester", Salary = 30000},
              new Employee { Id = 3,  Name = "Ramos", JobTitle = "DBA", Salary = 70000}
        };
        dataListEmployees.DataSource = employees;
        dataListEmployees.DataBind();

    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <h2>List Of Employees</h2>
        <asp:DataList ID="dataListEmployees" runat="server"
            ItemType="Employee">
            <ItemTemplate>
                <b><%# Item.Id %> </b>
                <br />
                <%# Item.Name%>
                <br />
                <%# Item.JobTitle %>
                <br />
                <%# Item.Salary %>
                <p />
            </ItemTemplate>
        </asp:DataList>
    </form>
</body>
</html>

Html Encoding in Data Binding Expression

You can now HTML-encode the result of data-binding expressions. Add a colon (:) to the end of the <%# prefix that marks the data-binding expression.
<asp:TemplateField HeaderText="Address">
        <ItemTemplate><%#: Item.Address %></ItemTemplate>
</asp:TemplateField>

Model Binding

Model binding extends data binding in ASP.NET Web Forms controls to work with code-focused data access.

To configure a data control to use model binding to select data, you set the control's SelectMethod property to the name of a method in the page's code. So the data comes from a method within the page.

The data control calls the method at the appropriate time in the page life cycle and automatically binds the returned data. There's no need to explicitly call the DataBind method.

The following example shows how to bind data to GridView using a method in the page. I am using Employee class from previous example.


<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

    public List<Employee> GetEmployees()
    {
        List<Employee> employees = new List<Employee> {
              new Employee { Id = 1,  Name = "Ronaldo", JobTitle = "Programmer", Salary = 50000},
              new Employee { Id = 2,  Name = "Alanso", JobTitle = "Tester", Salary = 30000},
              new Employee { Id = 3,  Name = "Ramos", JobTitle = "DBA", Salary = 70000}
        };
        return employees;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <h2>List Of Employees</h2>
        <asp:GridView ID="gvEmployees" runat="server" SelectMethod="GetEmployees">
        </asp:GridView>
    </form>
 </body>
</html>

Filtering Data

To filter the returned data, parameters have to be added to the select method. These parameters will be populated by the model binding at run time.

From where the value for parameter is taken can be specified using Value Providers, which are attributes.

The following table lists attributes related to different value providers.

The following example displays details of employees whose salaries are more than the given value. It uses ControlAttribute to copy value entered in textbox to parameter and uses it to filter data.

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

    public List<Employee> GetEmployees()
    {
        List<Employee> employees = new List<Employee> {
              new Employee { Id = 1,  Name = "Ronaldo", JobTitle = "Programmer", Salary = 50000},
              new Employee { Id = 2,  Name = "Alanso", JobTitle = "Tester", Salary = 30000},
              new Employee { Id = 3,  Name = "Ramos", JobTitle = "DBA", Salary = 70000}
        };
        return employees;
    }

    public List<Employee> GetSelectedEmployees( [System.Web.ModelBinding.Control ("txtSalary")] string salary )
    {
        if (Page.IsPostBack)
        {
            var employees = GetEmployees();
            return employees.FindAll(e => e.Salary >  Int32.Parse(salary));
        }
        else
            return null;           
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <h2>List Of Employees</h2>
        Minmum Salary : <asp:TextBox ID="txtSalary" runat="server"></asp:TextBox>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
        <p />
        <asp:GridView ID="gvEmployees" runat="server" SelectMethod="GetSelectedEmployees">
        </asp:GridView>
    </form>
</body>
</html>

Other Enhancements

Apart from what I mentioned above, the following are other interesting enhancements in ASP.NET 4.5.

Here are other importance resources to get more details about Asp.Net 4.5.

Keep Learning! Keep Growing!