A Famous Developer Quote says: Walking on water and developing software from a specification are easy if both are frozen. Building Complex Applications with ASP.net Can be a challenge but becomes a lot easier with DataControls. Let´s join Paul Cociuba in the 4th Video of his series and see what he has to tell us…


One of the most powerful features of ASP.net WebForms are the DataControls. These controls, allow developers to build complex applications with rich interfaces for displaying and interacting with data, coming from various data-sources, such as databases, files or even web-services. In this Video, we will create ASP.net WebForms pages, that will be using DataControls to interact with the entity classes we defined in the previous tutorial.

  • [2:20] - In the previous Video, we have created a Categories.aspx page with a ‘Category Listings’ heading. 
    • Changing the <h2> starting tag,# that surrounds the text, to a <strong> tag results in Visual Studio changing the matching end tag.
  • [2:50] - Another nice feature of VS 2012 is the fact that is automatically adds quotes to all attributes that need them.
  • [5:05] – Let´s look at the attributes in the GridView control. Two new attributes were introduced in ASP.net 4.5:
    • ItemType attribute tells the GridView control, what kind of objet types it will be displaying and allows it to use strongly typed data binding
    • SelectMethod Attribute, allows developers to point the GridView directly to a method, in the code behind the page.
  • [7:05] – Once the GridView markup is added, we Need to create the code-behind needed for the page to work.
    • Following this, the GetCategories method can be written. This method returns an IQueryable<ToyCategory> - a query which, when executed will return a collection of ToyCategory objects.
  • [8:35] – To help Entity Framework generate the database tables from our entities, we can use of attributes that come from the System.ComponentModel.DataAnnotations namespace.
    • The [Key] attribute indicates to Entity Framework which property definition of the class should be used as a primary key for the table.
  • [11:00] – The completed sample, will result in the ProductContext object being created and a call to the ProductModelInitializer class instance being issued. This will populate the database tables with the sample data, which is visible when inspecting the tables via the SQL Server Manager.
  • [13:00] – To allow the inspection of the details of each ToyCategory object, we create a new page, called CategoryDetails.aspx.
  • [14:20] – The DetailsView control also has the ItemType and SelectMethod attributes, that allows us to specify which object type the control is to be bound to, and what method in the code-behind is to called 
  • [15:35] – Let´s build the GetCategory code-behind method that will be used by the DetailsView control. Since this method has to select a single ToyCategory object from the database, we will use a parameter that will be passed into the method telling it which object to select.
  • [18:10] – To complete the DetailsView markup code, we  will add the Eval statements – which represents the old way of doing data-binding in ASP.net webforms. 
    • This type of data-binding is not strongly typed as the compiler treats the variable names as strings – and a spelling mistake will cause a runtime error – not a compile time error.
  • [20:30] – Let´s link the GridView control added previously to the DetailsView by adding a template field in the GridView. The template field will contain a hyperlink control which again uses the old data-bindings syntax: Eval(“CategoryID”, CategoryDetails.aspx={0} .
    • This statement will search for a property called CategoryID, evaluate its value and replace the {0} in the second parameter by the value.
  • [22:20] – running the project will display new hyperlinks in each of the rows displayed by the GridView, allowing us to construct a dynamic query string which will be used by the CategoryDetails.aspx page to find the element to display.

The next video of the series will focus on replacing the old fashion databinding Eval statements with newer constructs that will allow for strongly-typed databinding and eliminate the possibility of runtime errors.


Original content from Paul Cociuba; posted by MSPFE Editor Aydin Aslaner