Tutorial Series on Model Binding with ASP.NET Web Forms

Tom FitzMacken - MSFT

I have written a tutorial series that shows how to use model binding with ASP.NET Web Forms. You might be familiar with the model binding concept from ASP.NET MVC, but with ASP.NET 4.5, model binding is now available in Web Forms applications. Model binding makes it very easy to create and maintain data-rich web pages. A lot of the manual steps of correlating elements with data properties are performed automatically. When you use model binding with dynamic data templates, you can quickly add or revise properties in your data model and those properties are correctly rendered and processed in the web forms throughout your site.

The series contains the following topics:

  1. Retrieving and displaying data
  2. Updating, deleting, and creating data
  3. Sorting, paging, and filtering data
  4. Integrating JQuery UI Datepicker
  5. Using query string values to filter data
  6. Adding business logic layer

On data-bound server controls, such as GridView, DetailsView, ListView or FormView, you specify the name of a data method for the properties:

  • SelectMethod
  • UpdateMethod
  • DeleteMethod
  • InsertMethod

Within those methods, you use a data access technology (like Entity Framework) to perform the required data operations. In methods that update the data, you can take advantage of the TryUpdateModel method to automatically apply the bound values from the web forms to the data model.

You can also use several new attributes to specify the source of a parameter value. These attributes are in the System.Web.ModelBinding namespace, and include:

  • Control
  • Cookie
  • Form
  • Profile
  • QueryString
  • RouteData
  • Session
  • UserProfile
  • ViewState

For example, applying the QueryString attribute to a parameter means that the value of that parameter is retrieved from a query string value with the same name.

The tutorial series shows how to add data methods in a code-behind file for a web page, or in a separate class that manages the data operations for a site. Using a separate class enables you to centralize the data logic and re-use methods in multiple pages.

I hope the tutorial series is useful to you, and I welcome feedback.


Discussion is closed.

Feedback usabilla icon