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.

786 x APPENDIX A EXERCISE ANSWERS<br />

DropDownList control, which you can accomplish with the follow<strong>in</strong>g code <strong>in</strong> the Page_Load<br />

method:<br />

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

Protected Sub Page_Load(sender As Object, e As EventArgs) H<strong>and</strong>les Me.Load<br />

If Not Page.IsPostBack Then<br />

Dim genreId As Str<strong>in</strong>g = Request.QueryStr<strong>in</strong>g.Get("GenreId")<br />

If Not Str<strong>in</strong>g.IsNullOrEmpty(genreId) Then<br />

DropDownList1.DataB<strong>in</strong>d()<br />

Dim myItem As ListItem = DropDownList1.Items.F<strong>in</strong>dByValue(genreId)<br />

If myItem IsNot Noth<strong>in</strong>g Then<br />

myItem.Selected = True<br />

End If<br />

End If<br />

End If<br />

End Sub<br />

C#<br />

protected void Page_Load(object sender, EventArgs e)<br />

{<br />

if (!Page.IsPostBack)<br />

{<br />

str<strong>in</strong>g genreId = Request.QueryStr<strong>in</strong>g.Get("GenreId");<br />

if (!str<strong>in</strong>g.IsNullOrEmpty(genreId))<br />

{<br />

DropDownList1.DataB<strong>in</strong>d();<br />

ListItem myItem = DropDownList1.Items.F<strong>in</strong>dByValue(genreId);<br />

if (myItem != null)<br />

{<br />

myItem.Selected = true;<br />

}<br />

}<br />

}<br />

}<br />

Only when the page loads from a new request (<strong>and</strong> not from a postback) does this code fire. The<br />

code then tries to f<strong>in</strong>d a GenreId <strong>in</strong> the query str<strong>in</strong>g. If it can f<strong>in</strong>d it, it tries to f<strong>in</strong>d an item with that<br />

requested value <strong>in</strong> the DropDownList. Because the DropDownList control hasn’t been data bound<br />

yet it doesn’t conta<strong>in</strong> any items. Therefore, you need to call DataB<strong>in</strong>d() first. This gets the genres<br />

from the database us<strong>in</strong>g EF <strong>and</strong> puts them <strong>in</strong> the DropDownList. Once that’s done <strong>and</strong> the item is<br />

found <strong>in</strong> the Items collection, it’s made the active item by sett<strong>in</strong>g its Selected property to True/<br />

true. The SqlDataSource control watches this DropDownList so when the data source gets its<br />

reviews, it does so for the correct genre.<br />

Exercise 3 Solution<br />

The various data-bound controls can raise exceptions that you can h<strong>and</strong>le <strong>in</strong> their event h<strong>and</strong>lers.<br />

Once you have dealt with the exception appropriately, you need to set the ExceptionH<strong>and</strong>led property<br />

of the e argument to True. The follow<strong>in</strong>g code snippet shows how a Label control is updated

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

Saved successfully!

Ooh no, something went wrong!