Telerik blogs

The new release of Telerik’s controls for ASP.NET AJAX which we shipped just yesterday includes a ton of great new features and capabilities. This post is the first of two posts in which I will talk about some of the improvements coming with the new RadControls’ suite for the .NET 4.5 framework.  

For the past four months our developers worked hard in order to integrate the .NET 4.5 model binding support into our data bound controls and now you can see the result. The following controls have full ModelBinding support, including selecting data, filtering data, CRUD operations and validation:

The ModelBinding support is a cool extension of the existing data binding model of the ASP.NET. This is the same model binding that was previously only found in ASP.NET MVC, however now it can be found in RadControls for ASP.NET Ajax as well. Together with the ModelBinding feature you can taste the flavor of the new strongly typed data controls implementation into the ASP.NET 4.5.  So let’s see why you should immediately download the new version of our controls and try the new features.

How to do databinding the old and new 4.5 way with RadGrid

If you use any of the data bound controls provided from us, in 99% of all cases there is need of template fields. Till now in order to show the data from a datasource into the template field you you were forced to leverage the late-bound expression by using Eval() or Bind(). For example, below we are using the Eval() helper method to data-bind the "CategoryName" property from an objects which is bound to RadGrid:

<telerik:GridTemplateColumn HeaderText="Category" UniqueName="CategoryID"  DataField="CategoryID" SortExpression="CategoryID">
    <ItemTemplate>
         <%# Eval("Category.CategoryName") %>
    </ItemTemplate>
</telerik:GridTemplateColumn>

When we perform two-way data-binding, we use the Bind() method:

<telerik:GridTemplateColumn HeaderText="Category" UniqueName="CategoryID" DataField="CategoryID" SortExpression="CategoryID">
   <EditItemTemplate>
       <telerik:RadComboBox Skin="Metro" ID="ComboBox1" runat="server" SelectMethod="GetCategories" DataValueField="CategoryID" DataTextField="CategoryName" SelectedValue='<%# Bind("CategoryID") %>'> 
       </telerik:RadComboBox>
    </EditItemTemplate>
</telerik:GridTemplateColumn>

On the other hand, the .NET 4.5 provides ability to enable strongly-typed data templates. To enable them the ItemType property needs to be set on the data bound control. After setting it two new types will be generated in the scope of the control’s template: Item and BindItem. For example the code snippet above could be rewritten as:

<telerik:GridTemplateColumn HeaderText="Category"  UniqueName="CategoryID" DataField="CategoryID" SortExpression="CategoryID">
   <ItemTemplate>
       <%# Item.Category.CategoryName %>
   </ItemTemplate>
   <EditItemTemplate>
         <telerik:RadComboBox Skin="Metro" ID="ComboBox1" runat="server" SelectMethod="GetCategories"  DataValueField="CategoryID" DataTextField="CategoryName" SelectedValue="<%# BindItem.CategoryID %>">
         </telerik:RadComboBox>
  </EditItemTemplate>
</telerik:GridTemplateColumn>

You could use these variables in data-binding expressions and when you want to get full intellisense support.

databinding-expressions

Is this cool? I think it is, but hold on, below I will show you another great feature available in all Rad databound controls for .NET 4.5.

How to Select, Page and Sort Data with Model Binding

To bind the databound RadControls via ModelBinding you need to set only the SelectMethod property to the name of the public method placed into the page's code-behind file:

<telerik:RadGrid ID="RadGrid1" GridLines="None" runat="server" AllowSorting="true" PageSize="10" AllowPaging="True" SelectMethod="GetProducts" AutoGenerateColumns="False">

The GetProducts method has the following declaration:

DataClassesDataContext context = new DataClassesDataContext();
public IQueryable<Employee> GetProducts()
{
    return from e in context.Employees
               select e;
}

Or it can be with parameters:

public IQueryable<Employee> GetProducts(string sortByExpression, int startRowIndex, int maximumRows, out int totalRowCount)
{
    ...
}

In the second signature you have to build a query which sorts and pages the datasource items. 

When we run the page, the data bound controls will call the above method and automatically retrieve the data and render it. The method has to return IEnumerable or IQueryable. Thus users could easily page and sort through the data within our bound controls. The benefits are that our controls will automatically add the appropriate sort and page operators onto an IQueryable<T> query before executing it.

For example, if SelectMethod returns data from Linq DataContext and if the paging of the bound control is turned on, the executed database query will return only items for the current page. If the bound control is sorted by a column, the sorting will also be executed on the database and the sorted result will be returned. That is because the Linq will optimize the query to perform the sort and page operation as part of the database query.

The model binding is a great feature introduced by Microsoft, but still there is some limitation when using it with RadControls. In the following documentation articles you can see current limitations  enclosed into the note paragraphs:

Conclusion

These two features were some of the most frequently requested by you for the past Q. I think that you will be very pleased when you start working with Strongly Typed data bound controls that support ModelBinding and will find that they make your life a lot easier and more comfortable. You can download and try them implemented in all data bound controls.

P.S. Some of you may be guessing that there is something that is missing here and they will be right. This is not the whole story and there will be another blog post coming soon which will uncover the mystery of model binding filtering, CRUD operations and validation.


About the Author

Radoslav Kirilov

is a software developer at one of Telerik’s ASP.NET AJAX teams. Ever since he joined the company in 2009, he has been working on the data-bound and date-picker controls. His interests are primarily concentrated on ASP.NET, AJAX, MVC, SQL and best practices.

Related Posts

Comments

Comments are disabled in preview mode.