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.

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

The PreRenderComplete event fires very late <strong>in</strong> the Page control’s life cycle, mak<strong>in</strong>g it an ideal<br />

place to put a l<strong>in</strong>e at the bottom of the event list. That way you can clearly see what set of events<br />

belong to each other, which <strong>in</strong> turn helps you to figure out what events are triggered dur<strong>in</strong>g page<br />

load or a postback.<br />

12. Save all your changes <strong>and</strong> open the page <strong>in</strong> the browser. In addition to the GridView with the<br />

available genres, you should also see a list with event names under the No PostBack head<strong>in</strong>g:<br />

Page_Load<br />

EntityDataSource1_ContextCreat<strong>in</strong>g<br />

EntityDataSource1_Select<strong>in</strong>g<br />

GridView1_DataB<strong>in</strong>d<strong>in</strong>g<br />

GridView1_RowCreated<br />

GridView1_RowDataBound<br />

GridView1_RowCreated<br />

GridView1_RowDataBound<br />

...<br />

GridView1_DataBound<br />

Page_PreRenderComplete<br />

— — — — — — — — -<br />

Note that the RowCreated <strong>and</strong> RowDataBound events are repeated multiple times — once for each<br />

genre from the database plus two more. You see later why that is. Click the button below the<br />

GridView to cause a postback. The No PostBack label won’t change, but the PostBack label now<br />

shows the follow<strong>in</strong>g list of event names:<br />

GridView1_RowCreated<br />

GridView1_RowCreated<br />

...<br />

GridView1_RowCreated<br />

GridView1_RowCreated<br />

Page_Load<br />

Button1_Click<br />

Page_PreRenderComplete<br />

— — — — — — — — -<br />

Click one of the column headers of the GridView to order the data it is display<strong>in</strong>g. Notice that the<br />

second label’s text is extended with a second set of event names. Each set is separated by a l<strong>in</strong>e of<br />

dashes, created by the Page_PreRenderComplete event h<strong>and</strong>ler.<br />

How It Works<br />

Technically, this exercise isn’t complicated. You set up a bunch of event h<strong>and</strong>lers for the various controls<br />

<strong>in</strong> your page. Inside the event h<strong>and</strong>ler you call a method that checks whether the page is currently<br />

load<strong>in</strong>g for the first time or is load<strong>in</strong>g due to a postback. To make it easy to add the name of<br />

the call<strong>in</strong>g method to the Label control, the h<strong>and</strong>lerName parameter of the method has a special<br />

CallerMemberName attribute applied, like this:<br />

<strong>VB</strong>.<strong>NET</strong><br />

Private Sub WriteMessage( Optional h<strong>and</strong>lerName As Str<strong>in</strong>g = "")<br />

C#<br />

private void WriteMessage([CallerMemberName] str<strong>in</strong>g h<strong>and</strong>lerName = "")

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

Saved successfully!

Ooh no, something went wrong!