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.

H<strong>and</strong>l<strong>in</strong>g Events x 563<br />

background-image: url(Images/SortAscend<strong>in</strong>g.png);<br />

}<br />

The .GridViewHeaderStyle th selector then stops the background image from repeat<strong>in</strong>g, positions<br />

the image near the top, <strong>and</strong> determ<strong>in</strong>es the background color <strong>and</strong> text alignment:<br />

.GridViewHeaderStyle th, .GridViewPagerStyle<br />

{<br />

background-color: #BCD1FE;<br />

background-repeat: no-repeat;<br />

background-position: 0 5px;<br />

text-align: left;<br />

}<br />

By mov<strong>in</strong>g your control style declarations to a separate sk<strong>in</strong> file that <strong>in</strong> turn is part of a theme, you have<br />

created a very flexible, ma<strong>in</strong>ta<strong>in</strong>able solution. If you want to see how the new styles are applied, open<br />

the source of the page <strong>in</strong> the browser us<strong>in</strong>g its View Source comm<strong>and</strong>. Instead of <strong>in</strong>l<strong>in</strong>e styles, the relevant<br />

class attributes are applied. If you want to change the layout of all the GridView controls <strong>in</strong> the<br />

Management section, all you need to do now is modify the relevant CSS <strong>in</strong> the Management.css file. If<br />

you need to make changes to other styles, don’t forget to add them to the GridView.sk<strong>in</strong> file first.<br />

Obviously, you can still tweak the controls at the page level. Though the sk<strong>in</strong> def<strong>in</strong>es the global<br />

look <strong>and</strong> feel of the GridView, you can still set <strong>in</strong>dividual properties on columns as you did with the<br />

ItemStyle-Width <strong>in</strong> the Genres page.<br />

Although styles, sk<strong>in</strong>s, <strong>and</strong> themes are powerful tools to style your web pages, you’ll f<strong>in</strong>d<br />

that they are often an all-or-noth<strong>in</strong>g solution. For example, if you create ItemStyle <strong>and</strong><br />

Alternat<strong>in</strong>gItemStyle elements (rather than us<strong>in</strong>g jQuery as you just did), they are applied to each<br />

<strong>and</strong> every row <strong>in</strong> the grid. What if you wanted to change the look <strong>and</strong> feel of just a few rows? Or<br />

what if you wanted to change some rows based on the actual data that the row is hold<strong>in</strong>g? You see<br />

how to accomplish conditional formatt<strong>in</strong>g <strong>and</strong> more, us<strong>in</strong>g event h<strong>and</strong>l<strong>in</strong>g, <strong>in</strong> the follow<strong>in</strong>g section.<br />

HANDLING EVENTS<br />

Previous chapters have covered how the <strong>ASP</strong>.<strong>NET</strong> controls can raise events. You learned how to h<strong>and</strong>le<br />

these events with event-h<strong>and</strong>ler code that you typically add to the page’s Code Beh<strong>in</strong>d file. For example,<br />

you wrote code to h<strong>and</strong>le a Button control’s Click event. Additionally, <strong>in</strong> the preced<strong>in</strong>g chapter,<br />

you learned how to react to various events — such as Insert<strong>in</strong>g <strong>and</strong> Inserted — that happen just<br />

prior to <strong>and</strong> after <strong>in</strong>teraction with the database. However, most controls expose a lot more events.<br />

A solid underst<strong>and</strong><strong>in</strong>g of the various events that fire dur<strong>in</strong>g a control’s life cycle <strong>and</strong> the order <strong>in</strong><br />

which they fire is important knowledge for an <strong>ASP</strong>.<strong>NET</strong> developer. Be<strong>in</strong>g able to hook <strong>in</strong>to the control’s<br />

life cycle, tweak<strong>in</strong>g parts of the output as you go, enables you to create flexible, dynamic web<br />

pages that do exactly what you want.<br />

To ga<strong>in</strong> an underst<strong>and</strong><strong>in</strong>g of the various events <strong>and</strong> the order <strong>in</strong> which they fi re, the next section<br />

expla<strong>in</strong>s the basic steps <strong>in</strong> the <strong>ASP</strong>.<strong>NET</strong> control life cycle. You won’t see every event that is fired <strong>in</strong><br />

the process, but <strong>in</strong>stead you see the ones you are most likely to use. Later sections then show you<br />

how to make use of these events to change the behavior of your web pages.

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

Saved successfully!

Ooh no, something went wrong!