03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

536 x CHAPTER 14 LINQ AND THE ADO.<strong>NET</strong> ENTITY FRAMEWORK<br />

The code <strong>in</strong> the ListView uses Eval <strong>and</strong> B<strong>in</strong>d statements to get data <strong>in</strong> <strong>and</strong> out of the Picture objects<br />

that you’re assign<strong>in</strong>g to the controls. <strong>ASP</strong>.<strong>NET</strong> <strong>4.5</strong> <strong>in</strong>troduces a new way to set up these b<strong>in</strong>d<strong>in</strong>gs, as<br />

you see next.<br />

Us<strong>in</strong>g Strongly Typed Data-Bound Controls<br />

When VS created the code for the ListView for you, it added B<strong>in</strong>d <strong>and</strong> Eval statements like this:<br />

<br />

...<br />

<br />

As you learned <strong>in</strong> the “How It Works” section of the preced<strong>in</strong>g exercise, B<strong>in</strong>d is for two-way data<br />

b<strong>in</strong>d<strong>in</strong>g (for <strong>in</strong>sert <strong>and</strong> edit scenarios) <strong>and</strong> Eval is for read-only scenarios. Notice how the name of<br />

the property (ToolTip <strong>in</strong> this example) is a literal str<strong>in</strong>g placed between quotes. Us<strong>in</strong>g str<strong>in</strong>g literals<br />

makes your code more prone to errors. First of all, it’s easy to misspell the name. Because a str<strong>in</strong>g<br />

cannot be checked by VS at development time, you won’t notice this error until you view the page <strong>in</strong><br />

the browser. Secondly, if you rename a property, the change is not picked up by the str<strong>in</strong>g literal.<br />

Fortunately, <strong>ASP</strong>.<strong>NET</strong> <strong>4.5</strong> now has a solution to deal with this problem. The data-bound controls<br />

(such as the ListView, the Repeater, the GridView, the DetailsView, <strong>and</strong> the FormView) have<br />

been turned <strong>in</strong>to strongly typed data-bound controls. They have been extended with an ItemType<br />

property that you can po<strong>in</strong>t to the type of object you’re assign<strong>in</strong>g to its data source. In the preced<strong>in</strong>g<br />

example, this type would have been PlanetWroxModel.Picture, the fully qualified name of the<br />

Picture class. Once you’ve set this property, the data-bound control gets two new properties, Item<br />

<strong>and</strong> B<strong>in</strong>dItem, that are of the type you assigned to the ItemType property. The first one serves as a<br />

replacement for Eval <strong>and</strong> the second one replaces B<strong>in</strong>d. By sett<strong>in</strong>g the ItemType property <strong>and</strong> us<strong>in</strong>g<br />

Item <strong>and</strong> B<strong>in</strong>dItem, you get the follow<strong>in</strong>g benefits:<br />

‰ IntelliSense now helps you f<strong>in</strong>d the correct property of the object you’re work<strong>in</strong>g with, as<br />

shown <strong>in</strong> Figure 14-17.<br />

FIGURE 14-17<br />

‰ When you now misspell the name of a property, or rename it later, you get an error <strong>in</strong> the<br />

Error List, giv<strong>in</strong>g you the chance to fix the problem before the page is viewed <strong>in</strong> the browser.<br />

‰ External tools (available for the full versions of Visual Studio) will correctly rename the item<br />

<strong>in</strong> the markup when you rename a property on your object.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!