Also read Avoid DataBinder.Eval in ASP.NET and improve Performance
For this demonstration, I am using Windows 7 and Visual Studio 2012.
Let’s first create a blank web site using Visual Studio 2012 with the name “StronglyTypedDataControlDemo” as shown below –
![create-site create-site](http://lh6.ggpht.com/-adp4S-WtPvQ/ULMnFMR21eI/AAAAAAAAAaQ/ZTSvSMzxJog/create-site%25255B2%25255D.png?imgmax=800)
Now let’s add a new item which uses web form in our blank website with the default name “Default.aspx”. Now add a Repeater control in our “Default.aspx” as shown below –
![repeaterinitialcode repeaterinitialcode](http://lh6.ggpht.com/-pxJbGDGbvSU/ULMnGBqq42I/AAAAAAAAAaY/JvwF5aEaeEA/repeaterinitialcode%25255B3%25255D.png?imgmax=800)
If you observe the code, we have a “Header Template” and “Footer Template” as well we our “Item Template”. The “Item Template” is currently blank. Let’s design the item template as it’s usually done –
![item-template-code item-template-code](http://lh4.ggpht.com/-RWGryuNWjDo/ULMnGwFBgJI/AAAAAAAAAag/8uHDDJeNafc/item-template-code%25255B3%25255D.png?imgmax=800)
Now let’s add a new database “ExampleDB” and add a new table in the same database with the name “Customers” with following columns specifications –
![image image](http://lh6.ggpht.com/-wTmqEqaSIOY/ULMnH7lr7jI/AAAAAAAAAao/V6hioOWmkxk/image%25255B2%25255D.png?imgmax=800)
Add a new class, which is “LINQ to SQL” in our web site with the name “CustomerInformation.dbml” as shown below –
![linqdbml linqdbml](http://lh6.ggpht.com/-iz1QP3IzhOI/ULMnJP3ivEI/AAAAAAAAAas/mWZGLhml9No/linqdbml%25255B2%25255D.png?imgmax=800)
Now drag and drop customers table into our "CustomerInformation.dbml".
Add the following code into our code file -
![pageloadcode pageloadcode](http://lh6.ggpht.com/-fQ1WhByjONA/ULMnJ6yvo7I/AAAAAAAAAa4/dyr50A7jm6M/pageloadcode%25255B5%25255D.png?imgmax=800)
Run your web site and you will see the output as shown below -
![final1 final1](http://lh3.ggpht.com/-3FaIrzAnMRM/ULMnLaDCVBI/AAAAAAAAAbA/RmPU9V--hPA/final1%25255B2%25255D.png?imgmax=800)
Let's change the field name which we have passed to our "Eval" Expression and misspell it. After this change, compile the application and you will see the application gets compiled successfully.
![wrongcontname wrongcontname](http://lh6.ggpht.com/-5yCvavwFyVI/ULMnMqIsC6I/AAAAAAAAAbI/3WBJrLErpiw/wrongcontname%25255B6%25255D.png?imgmax=800)
If you observe, I have misspelled the "ContactName" to "ContacName" and my web site still gets complied. But if you run the web page, you will get an error as shown below -
![runtimeerror runtimeerror](http://lh3.ggpht.com/-ab8znXZ__LY/ULMnOOJncBI/AAAAAAAAAbM/A_G6RqyGCvc/runtimeerror%25255B2%25255D.png?imgmax=800)
To avoid similar issues where developers may misspell field names, ASP.NET 4.5 has come up with Strongly Typed Data Controls.
Now let's set a new property in our repeater control which is "ItemType" to "Customer" and replace the "Eval" expression with the "Item.ContactName" as shown below -
Note: Observe the intellisense when you set ItemType as well as when say Item followed by a dot(.) -
![itemtype itemtype](http://lh6.ggpht.com/-JsNns_yGwjU/ULMnO-y3EFI/AAAAAAAAAbU/1mVWVltkqW8/itemtype%25255B3%25255D.png?imgmax=800)
and
![item item](http://lh3.ggpht.com/-1jhRE2daLUw/ULMnQP7bztI/AAAAAAAAAbg/eS1fYLFVA48/item%25255B5%25255D.png?imgmax=800)
Now replace all Eval, with Item.ContactName, Item.State and Item.Country respectively and run your web page and you should see the same expected output as shown below -
![final1 final1](http://lh3.ggpht.com/-HYPQFtz7HyQ/ULMnRFzXHrI/AAAAAAAAAbo/pmhm9CLEWMs/final1%25255B5%25255D.png?imgmax=800)
Now try to misspell the column/field name, and you will instantly see a red line below the field and if you compile the application, you will get errors in "Error List" window as shown below -
![error error](http://lh5.ggpht.com/-aC_yYTZtmQo/ULMnSUtUvUI/AAAAAAAAAbw/NbbTDb1VzsQ/error%25255B2%25255D.png?imgmax=800)
How very convenient!
Summary - In this article, we have seen how to use the new Strongly Typed Data Controls compared to Eval expressions in ASP.NET 4.5.
Download the source code (GitHub)
Tweet
1 comment:
Here you provide the demo from which I could gain more from basic to advance.
Post a Comment