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.

Updat<strong>in</strong>g <strong>and</strong> Insert<strong>in</strong>g Data x 483<br />

‰ Click the Manage Reviews l<strong>in</strong>k <strong>in</strong> the menu on the left.<br />

‰ Select the right genre from the drop-down list at the top of the page. If you used the scripts<br />

from the preced<strong>in</strong>g chapter to create the data <strong>in</strong> your database, <strong>and</strong> you entered 1 for the<br />

GenreId when <strong>in</strong>sert<strong>in</strong>g the review, the genre is Rap <strong>and</strong> Hip-Hop.<br />

When you have found your review, you can click its title <strong>and</strong> you’ll be taken to AddEditReview<br />

.aspx, where you can change the review’s details aga<strong>in</strong>.<br />

How It Works<br />

Most of this exercise should be familiar by now. The DetailsView works the same for <strong>in</strong>sert<strong>in</strong>g as<br />

the DetailsView for genres you saw earlier. What’s different is how updates are h<strong>and</strong>led. The code<br />

<strong>in</strong> the Code Beh<strong>in</strong>d looks at the query str<strong>in</strong>g <strong>and</strong> if it f<strong>in</strong>ds an Id query str<strong>in</strong>g parameter, it flips the<br />

DetailsView <strong>in</strong>to edit mode:<br />

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

If Request.QueryStr<strong>in</strong>g.Get("Id") IsNot Noth<strong>in</strong>g Then<br />

DetailsView1.DefaultMode = DetailsViewMode.Edit<br />

End If<br />

C#<br />

if (Request.QueryStr<strong>in</strong>g.Get("Id") != null)<br />

{<br />

DetailsView1.DefaultMode = DetailsViewMode.Edit;<br />

}<br />

When the control is <strong>in</strong> edit mode, it knows what to do. It calls the SqlDataSource <strong>and</strong> requests its<br />

data. The SqlDataSource <strong>in</strong> turn retrieves the ID of the review from the query str<strong>in</strong>g, accesses the<br />

database, <strong>and</strong> then returns the correct review, which is displayed on the page. When you subsequently<br />

click the Update l<strong>in</strong>k, the SqlDataSource fires its UpdateComm<strong>and</strong> to send the changes to the database.<br />

This exercise provides a nice foundation for the follow<strong>in</strong>g exercise, where you extend the<br />

DetailsView by implement<strong>in</strong>g custom templates with validation controls <strong>and</strong> set up various event<br />

h<strong>and</strong>lers to respond to the control’s events.<br />

Right now, the page with the DetailsView looks quite dull. It would look a lot better <strong>and</strong> be easier<br />

to use if it had the follow<strong>in</strong>g features:<br />

‰ A text area <strong>in</strong>stead of a s<strong>in</strong>gle-l<strong>in</strong>e text box for the Summary <strong>and</strong> Body fields<br />

‰ A drop-down list for the genre filled with the available genres from the database<br />

‰ Automatic updat<strong>in</strong>g of the UpdateDateTime column<br />

‰ Validation controls to stop you from leav<strong>in</strong>g required fields empty<br />

‰ Automatic redirection to the Reviews.aspx page after an item has been <strong>in</strong>serted or updated<br />

The next exercise shows you how to implement all of these features.

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

Saved successfully!

Ooh no, something went wrong!