Assigning new users to a role The CreateUserWizard
Thursday, April 26th, 2007Assigning new users to a role The CreateUserWizard control doesn t automatically assign a new user to a role. If you want to put all the people who sign up through the CreateUser Wizard control into a specific role, add a little code. I know I haven t talked about code in this book, and now is not the time to get into details of writing code. However, for this example, I can just show you what to type. This technique only works if you ve already enabled Roles in the Web Site Administration Tool as discussed in Chapter 3. Also, you ll have to remember exactly how you spelled your role name, as it won t appear in any IntelliSense menu. If you want your CreateUserWizard control to automatically assign every new user to a specific role in your site, follow these steps: 1. In design view, select the CreateUserWizard control. Its name appears in the Properties sheet when selected. 2. In the Properties sheet, click the Events (lightning bolt) button. 3. Double-click the CreatedUser event. You re taken to the code-behind page for the page you re designing; the cursor is already in the event procedure, right where you type its code. 4. Type the following at the cursor position, substituting your own data for the italics, as follows: Roles.AddUserToRole(ctrlName.UserName, roleName ); ctrlName refers to the name of your CreateUserWizard control. That would likely be CreateUserWizard1 if you let VWD name the control. When you type a roleName, make sure you spell it exactly as you did when creating the role in the Web Site Administration Tool. The code given here shows what my example looked like after adding a line of code to add the new user to my SiteMembers role. protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e) { Roles.AddUserToRole(CreateUserWizard1.UserName, SiteMembers ); } Typing code isn t like typing in English. In code you have to get every dot, comma, parenthesis, quotation mark, and semicolon exactly right. Spelling and blank spaces count a lot too. The code won t work if you don t type the line exactly right. 5. Close and save the code-behind page. Chapter 7: Working with ASP.NET Controls 133
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision ecommerce web hosting services