Strongly Typed Data Binding
Paul Cociuba, continues to take us through the ASP.Net 4.5 Journey. In his fifth video, he will guide us through the “Strongly typed Data Binding”. Paul, the stage is yours…
This video will continue to explore data-binding in ASP.net 4.5 by looking at improvements brought on by strongly typed data-binding. Historically, .Net languages started as strongly typed languages. The VS product Team, has extended the strongly typed concept to data-binding, which allows the application to know what the type of data it will be binding, at compile time rather than at runtime.
- [0:40] Taking a look at the solution we have been using so far, modified to contain a Products page (where the web-application will be listing all the products ) and ProductDetails page (which will list the details for a particular product).
- The first page (Products.aspx) uses a GridView control whose fields are bound to various properties of the Product entity and a template field with a HyperLink control to construct the query strings needed to navigate to the ProductDetails.aspx page
- [1:12] The ProductDetails uses a FormView control with a ItemTemplate to display the selected Product entity’s details using Eval Statements.
- [1:45] Let´s review of the code behind methods for the two newly added pages:
- the GetProducts() method which is used to retrieve a query which when executed will return the list of products. It is up to the GridView control to actually run the query retuned by the methd via IQueryable<Products> return object
- the GetProduct(int id) method that uses the Find method exposes by the Products property of the ProductsContext object to retrieve a product with a given ID.
- [2:15] – Running the solution, shows the listings of the products via the Products.aspx page. Each row has a hyperlink with the url set to the ProductsDetails.aspx page with the correct query string indicating the id of the product.
- [3:08] – A limitation of the old data-binding style, which we have been using until now, and which uses Eval statements is that all Eval parameters are strings, and the property names are looked up at runtime. Binding is done at runtime with no type checking. Should the property not be found, no errors will be issued at compile time, but the will be raised when the page is run.
- [4:12] – Replacing the Eval statements by Item. statements, which return strongly typed data is the new way of doing strongly typed data binding. We have already indicated the type of the data to bind to to the DataControl via the ItemType property.
- [5:58] – Replacing the Hyperlink control with the double Eval statement with an Item.statement is the next step.
- The composition of the query string now becomes much simpler since we are concatenating the static (as in non-changing) part of the query string with the parameter value from the Item statement – it is simple string concatenation, and the property names are checked by the compiler at compile time.
- [7:45] – in ProductDetails.aspx we can replace all the Eval statements by strongly typed Item. Statements.
- We can further pull in properties from entities related to the Product entity – like the ToyCategory entity – via the Category property on the Product entity which is a reference to the corresponding ToyCategory object. This allows us to write an Item.Category.Name statement which will return the name of the category. This will make Entity Framework build a SQL statement where the name of each category associated to a Product entity will also be loaded.
- [10:10] – Running the Categories.aspx page results in the same output, but the data binding is strongly typed.
- The CategoryDetails.aspx page shows that the Last Updated field is now nicely formatted since the Item.LastUpdated returned a DateTime object that could be formatted. In ProductsDetails.aspx page, the page pulls in the name of the Category that the product belongs to.
The next video of the tutorial will be focusing on the implementation of the rest of the CRUD pattern. We will be adding support for Creating, Updating and Deleting products and category objects using strongly typed data-binding.
Original content from Paul Cociuba; posted by MSPFE Editor Aydin Aslaner