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.

Other Ways of Deal<strong>in</strong>g with Profile x 673<br />

Management section that enables you to manage the profiles of the users that registered at your site.<br />

When you do modify other users’ profiles, be sure to call the Save method when you’re done. As you<br />

learned earlier, changes to the profile are normally persisted <strong>in</strong> the database automatically. However,<br />

this applies only to the profile of the current user. To change <strong>and</strong> persist the previously retrieved profile,<br />

you can use this code:<br />

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

theProfile.Bio = "New Bio for the Carmen account here"<br />

theProfile.Save()<br />

C#<br />

theProfile.Bio = "New Bio for the Carmen account here";<br />

theProfile.Save();<br />

In the follow<strong>in</strong>g exercise, you put some of this <strong>in</strong>to practice when you show the name of the user<br />

who created a specific photo album, together with the biography of the user.<br />

TRY IT OUT<br />

Work<strong>in</strong>g with Other Users’ Profiles<br />

The Default.aspx page <strong>in</strong> the PhotoAlbums folder displays the pictures <strong>in</strong> a specific photo album.<br />

You can’t see which user created the photo album, so that would be a nice new feature. And to further<br />

improve the page, you can also display the user’s biography. In this Try It Out, you see how to implement<br />

both features.<br />

1. From the PhotoAlbums folder, open the Default.aspx page <strong>in</strong> Markup View. Scroll down <strong>and</strong><br />

locate the two breaks <strong>and</strong> the HyperL<strong>in</strong>k to edit the album you added earlier. Just before the<br />

breaks <strong>and</strong> the HyperL<strong>in</strong>k control, drag a PlaceHolder control from the Toolbox <strong>and</strong> set its ID to<br />

PhotoAlbumDetails. Inside this PlaceHolder, drag two Label controls <strong>and</strong> then modify the code<br />

manually so it ends up like this:<br />

<br />

<br />

Photo Album Details<br />

Created by:<br />

<br />

About this user:<br />

<br />

<br />

<br />

<br />

2. Switch to the Code Beh<strong>in</strong>d of the page (press F7) <strong>and</strong> locate the DataBound event h<strong>and</strong>ler for the<br />

ListView control. Right after the nested if statement that hides the HyperL<strong>in</strong>k control when the<br />

user doesn’t have the necessary permissions, add these l<strong>in</strong>es of code that retrieve the profile for the<br />

user who created the photo album <strong>and</strong> then update the relevant labels:<br />

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

EditL<strong>in</strong>k.Visible = False<br />

End If<br />

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

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

Saved successfully!

Ooh no, something went wrong!