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.

Introduction to User Controls x 293<br />

Normally, when you put an element <strong>in</strong>side an element to l<strong>in</strong>k it, the browser draws a border<br />

around the image. The border is usually blue for unvisited l<strong>in</strong>ks <strong>and</strong> purple for l<strong>in</strong>ks you have visited<br />

before, though you could have some CSS code target<strong>in</strong>g a elements that overrides these default colors.<br />

To remove that border completely, you need to add the follow<strong>in</strong>g CSS:<br />

img<br />

{<br />

border: none;<br />

}<br />

When you add a user control to a page, VS usually refers to the control us<strong>in</strong>g a relative path. In this<br />

exercise, this path first conta<strong>in</strong>ed two dots (..) to <strong>in</strong>dicate the parent folder, followed by the Controls<br />

folder, <strong>and</strong> f<strong>in</strong>ally by the name of the control:<br />

<br />

By chang<strong>in</strong>g the two dots to the tilde symbol, it becomes easier to move your pages around <strong>in</strong> your site<br />

because the Src attribute now always po<strong>in</strong>ts to the Controls folder at the application’s root, no matter<br />

where the page that consumes the control is located. If your Src attribute already conta<strong>in</strong>ed the tilde,<br />

you don’t have to change anyth<strong>in</strong>g.<br />

Though the tilde syntax makes your pages with user controls a little easier to manage, there is an<br />

even easier way to register your user controls sitewide.<br />

Sitewide Registration of User Controls<br />

If you have a control that you expect to use quite often on separate content pages <strong>in</strong> your site, like<br />

the banner <strong>in</strong> the previous examples, you can register the control globally <strong>in</strong> the Web.config file.<br />

This way it becomes available throughout the entire site, without the need to register it on every<br />

page. The follow<strong>in</strong>g exercise shows how to do this.<br />

TRY IT OUT<br />

Register<strong>in</strong>g User Controls <strong>in</strong> the Web.config File<br />

In this exercise, you register the Banner.ascx user control <strong>in</strong> the Web.config file. You can then<br />

remove the @ Register directive from the master page because it isn’t needed anymore. After you<br />

have changed the Web.config file, add<strong>in</strong>g the same user control to other pages will no longer add the @<br />

Register directive to the page.<br />

1. Open the Web.config file from the root of the site. If you’re familiar with versions of <strong>ASP</strong>.<strong>NET</strong><br />

prior to .<strong>NET</strong> 4, you may f<strong>in</strong>d the Web.config worry<strong>in</strong>gly empty. But don’t worry; the functionality<br />

it <strong>in</strong>cluded has not been removed from .<strong>NET</strong>. The configuration <strong>in</strong>formation placed <strong>in</strong> the website’s<br />

Web.config file has now been moved to the central Web.config <strong>and</strong> Mach<strong>in</strong>e.config files<br />

that apply to the entire mach<strong>in</strong>e <strong>in</strong>stead. This gives you a much cleaner configuration file, mak<strong>in</strong>g it<br />

easy to focus on your own stuff that you put <strong>in</strong> there.<br />

2. Locate the element that you used <strong>in</strong> Chapter 6 to apply the theme, <strong>and</strong> with<strong>in</strong> its tags<br />

add the follow<strong>in</strong>g bolded code that conta<strong>in</strong>s a element with a child element.

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

Saved successfully!

Ooh no, something went wrong!