1 / 28

Using Personalization

Using Personalization. Creating user profiles Creating user profiles Retrieving, changing, and saving profile properties Using validation controls Using the Forms Designer. Introduction.

aimeer
Download Presentation

Using Personalization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using Personalization Creating user profiles Creating user profiles Retrieving, changing, and saving profile properties Using validation controls Using the Forms Designer

  2. Introduction • When you configure your Web site to support membership, VWDautomatically creates database tables to store information aboutuser accounts. • However, it creates only the bare-minimum number of fieldsneeded, such as User Name, E-mail Address, and Password. • If you want tostore more information than that about each user, such as name, address,and phone number, you must define profile properties. • The basic idea in VWD is this: Every authenticated user has a profile that contains information about that user. In code, you can use the simple syntax Profile.propertyname to get, or store, information about each user. • The keyword Profile (with an uppercase P) always means “whichever user happens to be viewing this page.”

  3. Setting up Profiles • For each person who creates an account onyour Web site you should include at least the following data: • FirstName • LastName • Address1 • Address2 (optional second line of street address). • City • StateProvince • ZIPPostalCode • Country

  4. Setting up user profiles • To define user profiles, you need to manually edit the Web.config file in theroot of your Web site: • Open Web.config for editing by just double-clicking its icon.Typically you’ll see that filename located near the bottom of SolutionExplorer. • Just above the closing </system.Web> tag, type • <profile>and press Enter. • A closing </profile> tag is added automatically. • With the cursor placed between the <profile> and </profile> tags,type: • <properties>and press Enter. Once again, the closing </properties> tag is entered • Between the <properties> and </properties> tags you need to typea tag for each property you want to define using the following syntax: • <add propertyName />where propertyName is the name of the profile property.

  5. Changes in the Web.config • Type the following: • <profile> • <properties> • <add name=”FirstName” type=”System.String”/> • <add name=”LastName” type=”System.String”/> • <add name=”Address1” type=”System.String”/> • <add name=”Address2” type=”System.String”/> • <add name=”City” type=”System.String”/> • <add name=”StateProvince” type=”System.String”/> • <add name=”ZIPPostalCode” type=”System.String”/> • <add name=”Country” defaultValue=”USA”type=”System.String”/> • </properties> • </profile>

  6. Your Web.config should look like

  7. Another Sample of the Web.config

  8. How to let Users enter their properties • The whole purpose of creating profile properties is to store informationabout users (site members). You want each user to type in his or herown profile properties. To do so, you need to provide users with a fill-in-theblanksform. • A form is basically a Web page with controls for entering and editing data. To create a Web form for entering profile properties, justcreate a new, empty .aspx page (For Example CreateProfile.aspx) to let users enter profile information in their ownaccounts. • Steps • In Solution Explorer, right-click the folder in which you want to storethe new page and choose Add New Item. • Good idea to put it in the MemberPages folder. • In the Add New Item dialog box, click Web Form. • Enter a filename for your page (CreateProfile.aspx). • Click the Add button. • If you opted to use a Master Page, choose your master, and then click OK.

  9. Make the page to look like:

  10. Names for your VARIABLES

  11. Typing code to an event • Add labels and textboxes • Add a button • Fix their positions by using LayoutPositionAbsolute • Double click on the button

  12. Determining where to putthe profile information • The next thing to consider is when users enter profile information. You can provide a link to the page from any place you want (though a goodtime to grab users is right after they finish successfully creating their individualuser accounts). • If you used a CreateUserWizard control to let users setup accounts, the job is easy. You just set the ContinueDestinationPageURLproperty of the CreateUserWizard control to the CreateProfile.aspxpage. • Open the page in design view, click the control to select it, and then scroll through the (lengthy) set of properties until you get to ContinueDestinationPageURL. There you should click the property, click the Build button, navigate to the CreateProfile.aspx page, and click OK.

  13. Example

  14. Think About Security • Think about Web site securitythe whole way through the above scenario. • You don’t want anonymous users tohave any access to profiles, so put the CreateProfile.aspx page in the MemberPages folder. That way an anonymous user can’t get to that page. Theonly way to get to that page is by first successfully creating a valid useraccount. • The word “successfully” is key there. I wouldn’t want people to sneak aroundsetting up accounts. But that’s not going to happen; only users who successfullycreate an account can reach the ContinueDestinationPageURL. • Thatrestriction is built right into the logic of the CreateUserWizard • If you’re going to allow users to enter profile information, you’ll also have tolet them change their own information. We haven’t done anything about thatyet. But you can do so pretty quickly.

  15. Letting users edit their profiles • You’ve only created a form that can store information in a user’s profile.You haven’t come up with a means to retrieve a user’s profile properties. • If wewant users to edit their own profiles, we need a page that can retrieve theprofile information, show it on a form for the user to view (or edit), and ifneed be, send any changes back to the database. • Getting data from the profile to the text box is just the opposite of gettingdata from the text box to the profile. That is, rather than making the profileproperty equal to the text box’s text, you make the text box text equal to theprofile property. • For example, to copy the user’s FirstName profile propertyto a control named txtFirstName, the C# statement is: • txtFirstName.Text = Profile.FirstName;

  16. Letting users edit their profilesContinue • You could whip this profile-retrieval page together quickly just by making acopy of CreateProfile.aspx and renaming it to EditProfile.aspx. • To dothis, open the EditProfile.aspx page, then double-click some empty spaceon the page background (or the content placeholder). Or you can justdouble-click EditProfile.aspx.cs in Solution Explorer. Either way, thecode-behind page opens. • You want the profile properties to be loaded into the page as soon as thepage opens so the user sees their information and can make changes. To executecode as soon as a page opens, put that code in the page’s Page_Loadevent handler. • There’s just one catch. With ASP.NET programming, every time a user doessomething on your page, a postbackgets sent to the Web server. The postbackcauses the Page_Load event handler to execute again — which is fine insome cases — sometimes (as in this situation) the Page_Load code shouldexecute only when the user first opens the page — not every time they click abutton on the page.

  17. Letting users edit their profilesContinue SO ADD THE CODE :

  18. Where are profile properties stored? • The profile properties are stored in the samedatabase as the rest of the membership stuff —the database you see when you click theDatabase Explorer tab instead of SolutionExplorer. • Specifically, the profile properties arestored in the aspnet_Profile table. But theprofiles aren’t stored in a traditional manner, soyou don’t want to go rummaging around in thattable unless you really know what you’re doing. • NOTE : If you put your CreateProfile and EditProfile pages in a protectedfolder, don’t be surprised if your login page opens when you try to vieweither one in a Web browser. When you’re in a Web browser, you’re justanother user. Like everyone else, you have to log in to a valid user accountbefore you can access any page in your site’s protected folder(s).

  19. Using Validation Controls • The sample forms we presented earlier have one weakness: • They accept anything the user types into the boxes. In fact, they accept theform even if the user leaves every text box empty. That might be okay if youdon’t want to force users to enter personal information. But if getting profiledata from users is important, you may have to reject empty fields or fieldsthat contain meaningless data. • The ASP.NET validation controls, available in the Validation section of theToolbox, make it easy to verify form data . To use a validationcontrol, you first need a place to put it on your form. You’ll want to put itnear the control you’re validating, because the control will display an errormessage if the user’s entry isn’t valid.

  20. Properties of Validation Controls • Most validation controls have certain properties that must be set in order forthe validation to work. The main properties are: • ControlToValidate: Specify the name (ID) of the Textbox control thatshould be validated. • Display: Choose Dynamic so the error message takes up no space on thescreen unless a user’s entry fails the validity test. • ErrorMessage: Any text you type here is used in a ValidationSummarycontrol (if any) on the current page. This is not the error message thatappears near the control. • Text: Whatever you type here appears next to the control, but only if auser’s entry fails to pass validation. It can be a simple asterisk (*) or amore descriptive message such as Required Field!.

  21. RequiredFieldValidator • The most commonly used validation control is theRequiredFieldValidator, which prevents users from leaving a text boxempty. • It’s super-easy to use: You just drag the control to your page, and thenset its ControlToValidate property to the programmatic name (ID) of thecontrol that you don’t want left blank. Example: • Add a third column to a table, and then drag a RequiredFieldValidator control to the cell to the right of my txtFirstName Textboxcontrol. • Set the ControlToValidate property to txtFirstName to ensure thatthe text box would not be left blank. • Set the Display property toDynamic, and the ErrorMessage to First name required. • Set the Text property to an asterisk (*) — which reduced the size ofthe control to an asterisk. • You can see the control selected, in the upperrighttable cell, in the following figure

  22. RequiredFieldValidatorContinue

  23. RequiredFieldValidatorContinue • In a Web browser, those properties play out as follows: • When the page first opens, the validation control is there but invisible. • If the user clicks the Submit button and the text box is empty, the validationcontrol shows its Text property (here it’s a red asterisk). • The ErrorText is displayed in a ValidationSummary control — provided that you’ve added one to your page a “ValidationSummary” section (See in a minute). • You can add as many validation controls to a page as you need to ensurequality data entry.

  24. RangeValidator • The RangeValidator lets you check an entered value to verify that it fallswithin some range of acceptable values. • It’s the same idea as other controlsin that you drag it to the page and set its ControlToValidate property tothe name of the control you want validated. • Then you set the MaximumValue and Minimum Value properties to define the range of acceptablevalues for the control.

  25. RegularExpressionValidator • A regular expressionis a symbolic way of defining complex validation criteria.You can do things like validate an entry against a pattern of characters, oradd complex “or” logic to a validation control. • The language for creating regularexpressions is extensive and beyond the scope of this course. • But there aresome ready-made ones that you can use without studying the whole languageof regular expressions. • Check http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcomregularexpressions.asp.

  26. CompareValidator • Use the CompareValidator to check a text box entry by comparing it to aknown value or the contents of another control. The most common use ofthis is when having a user enter a new password twice. • Use a CompareValidator to compare the contents of the two controls, and reject bothentries if they don’t match. • Here’s how to use one: • Drag a CompareValidator control to your form and drop it there. • Check the new control by setting its ControlToValidate property to • the name of the control. • Use the control to make a comparison of contents. • To compare your control’s contents to those of another control,set the ControlToCompare property to the name of the control towhich you’re comparing the current control. • To compare the entry to some other known value, set theValueToCompare property of the control to the comparison value.

  27. CustomValidator • Use a CustomValidator if none of the other validation controls can do whatyou need done — and you also happen to be a skilled programmer.If that’s your thing, here are your steps: • Drag a CustomValidator control to the page. • Set the control’s ControlToValidate property to the name of the controlyou want to validate. • Switch back to your form. • Outside the Properties sheet, double-click the CustomValidator controlyou dropped onto the page. • The code-behind file for the page opens with a new CustomValidator1_ServerValidate procedure all ready and waiting for you to type in yourcode. • Write your validation routine, close and save both files, then view thepage in a Web browser to test it out.

  28. ValidationSummary • The ValidationSummary control displays a summary of all failed validationsthat occurred on the form. • All you have to do is drop it on the form. Youdon’t even need to change its properties. • In Design view, the ValidationSummary is just a placeholder showing nouseful information. • In a Web browser, it initially is invisible. After the userclicks the Submit button, it lists theErrorMessageText of each control thatfailed validation. It’s simple to use, and handy for users.

More Related