03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

308 x CHAPTER 8 USER CONTROLS<br />

This code first tries to get the value from View State us<strong>in</strong>g ViewState("NavigateUrl") <strong>in</strong> <strong>VB</strong>.<strong>NET</strong> or<br />

ViewState["NavigateUrl"] <strong>in</strong> C#, which uses square brackets to access items <strong>in</strong> a collection. If the<br />

value that is returned is Noth<strong>in</strong>g or null, the getter returns the default value for the property: http://<br />

p2p.wrox.com.<br />

However, if the value is not Noth<strong>in</strong>g, it is cast to a str<strong>in</strong>g us<strong>in</strong>g CType <strong>in</strong> <strong>VB</strong>.<strong>NET</strong> <strong>and</strong> (str<strong>in</strong>g) <strong>in</strong> C#<br />

<strong>and</strong> eventually returned to the call<strong>in</strong>g code. At the end, the NavigateUrl returned from the View State<br />

property is assigned to the HRef property of the anchor tag aga<strong>in</strong>, which is then used as the URL users<br />

are taken to when they click the image.<br />

View State Considerations<br />

Although View State is designed to overcome the problems of ma<strong>in</strong>ta<strong>in</strong><strong>in</strong>g state as outl<strong>in</strong>ed <strong>in</strong> the<br />

previous exercise, you should carefully consider whether or not you use it. The values you store <strong>in</strong><br />

View State are sent to the browser <strong>and</strong> back to the server on every request. When you store many<br />

or large values <strong>in</strong> View State, this <strong>in</strong>creases the size of the page <strong>and</strong> thus negatively impacts performance.<br />

Never store large objects like database records <strong>in</strong> View State; it’s often quicker to get the<br />

data fresh from the database on each request than pass<strong>in</strong>g it along <strong>in</strong> the hidden View State field if<br />

the amount of data that needs to be stored is large. Also, because the View State is stored with<strong>in</strong><br />

the page <strong>and</strong> is thus transferred over the wire, you shouldn’t use it to store sensitive values such as<br />

passwords.<br />

PRACTICAL TIPS ON USER CONTROLS<br />

The follow<strong>in</strong>g list provides some practical tips on work<strong>in</strong>g with user controls:<br />

‰ Don’t overuse user controls. User controls are great for encapsulat<strong>in</strong>g repeat<strong>in</strong>g content, but<br />

they also make it a little harder to manage your site because code <strong>and</strong> logic is conta<strong>in</strong>ed <strong>in</strong><br />

multiple files. If you’re not sure if some content will be reused <strong>in</strong> another part of the site, start<br />

by embedd<strong>in</strong>g it directly <strong>in</strong> the page. You can always move it to a separate user control later<br />

if the need arises.<br />

‰ Keep user controls focused on a s<strong>in</strong>gle task. Don’t create a user control that is able to display<br />

five different types of unrelated content with a property that determ<strong>in</strong>es what to display. This<br />

makes the control difficult to ma<strong>in</strong>ta<strong>in</strong> <strong>and</strong> use. Instead, create five lightweight controls <strong>and</strong><br />

use them appropriately.<br />

‰ When you create user controls that conta<strong>in</strong> styled markup, don’t hardcode style <strong>in</strong>formation<br />

like the CssClass for the server controls conta<strong>in</strong>ed <strong>in</strong> the user control. Instead, consider<br />

creat<strong>in</strong>g separate CssClass properties on the user control, which are then used to set the<br />

CssClass of your server controls. This improves the reusability of your user control, mak<strong>in</strong>g<br />

it easier to <strong>in</strong>corporate the control <strong>in</strong> different designs.

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

Saved successfully!

Ooh no, something went wrong!