German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


Create a User Profile in Regisration Page

Written by omerkamal on Dec 30, 2006
How to create user profile the time they get register?

Introducation:

I saw many questions in differnt forums about how to handel the Profile creation at when users register. Most users now knows about Login and Membership new Controles in ASP .NET 2.0 . But how to handle this special event is kind of different approach.

Explanation:

We will create a Registration Page where users will also provide us some optional entries. This is completly managed by ASP .NET 2.0 Profiling. Let us do it step by step.

  • Create a New Page Call it "GetRegistered.aspx" also select "Place code id separate file"
  • Open the Design view of the page
  • Throw the "CreateUserWizard" on the page
  • Click the Wizard from Top right Corner small errow ( this is where we can edit every server controle)
  •  Click the Link "Customize Create User Step" .
  •  Now open the source view of page. You noticed that it created alot of code in your page.
  •  Add some  more code to the Wizard as shown in the code below ( in bold)

<div align=center style="margin-top:100px">
<asp:CreateUserWizard ID="WizardCreateUser" runat="server" ContinueDestinationPageUrl="~/Default.aspx" OnCreatedUser="WizardCreateUser_CreatedUser">
<WizardSteps>
<asp:CreateUserWizardStep runat="server" >
<ContentTemplate>
<table border="0">
<tbody>
<tr>
<td align="center" colspan="2">
<asp:Label ID=top runat=server Text="Sign Up for Your New Account" Font-Bold="True" Font-Size="10pt"></asp:Label>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label></td>
<td align=left>
<asp:TextBox ID="UserName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="CreateUser">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label></td>
<td align=left>
<asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="CreateUser">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label></td>
<td align=left>
<asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" ControlToValidate="ConfirmPassword"
ErrorMessage="Confirm Password is required." ToolTip="Confirm Password is required."
ValidationGroup="CreateUser">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-mail:</asp:Label></td>
<td align=left>
<asp:TextBox ID="Email" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email"
ErrorMessage="E-mail is required." ToolTip="E-mail is required." ValidationGroup="CreateUser">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="QuestionLabel" runat="server" AssociatedControlID="Question">Security Question:</asp:Label></td>
<td align=left>
<asp:TextBox ID="Question" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="QuestionRequired" runat="server" ControlToValidate="Question"
ErrorMessage="Security question is required." ToolTip="Security question is required."
ValidationGroup="CreateUser">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="AnswerLabel" runat="server" AssociatedControlID="Answer">Security Answer:</asp:Label></td>
<td align=left>
<asp:TextBox ID="Answer" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="AnswerRequired" runat="server" ControlToValidate="Answer"
ErrorMessage="Security answer is required." ToolTip="Security answer is required."
ValidationGroup="CreateUser">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="center" colspan=2>
<asp:Label ID="Label3" runat="server" Font-Bold="True" Font-Size="10pt">Optional Informations</asp:Label>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label1" runat="server" >First Name:</asp:Label></td>
<td align=left>
<asp:TextBox ID="TextBoxFirstName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="Label2" runat="server" >Last Name:</asp:Label></td>
<td align=left>
<asp:TextBox ID="TextBoxLastName" runat="server"></asp:TextBox>
</td>
</tr>

<tr>
<td align="center" colspan="2">
<asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password"
ControlToValidate="ConfirmPassword" Display="Dynamic" ErrorMessage="The Password and Confirmation Password must match."
ValidationGroup="CreateUser"></asp:CompareValidator>
</td>
</tr>
<tr>
<td align="center" colspan="2" style="color: red">
<asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
</td>
</tr>
</tbody>
</table>
</ContentTemplate>
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep runat="server" />
</WizardSteps>
</asp:CreateUserWizard>
</div>

 Now, Right Click the page and go to "Code view" of the Page. add this code to the Page.

protected void WizardCreateUser_CreatedUser(object sender, EventArgs e)
{
ProfileCommon p = (ProfileCommon)ProfileCommon.Create(WizardCreateUser.UserName, true);

p.FirstName = ((TextBox)WizardCreateUser.CreateUserStep.ContentTemplateContainer.FindControl("TextBoxFirstName")).Text;

p.LastName = ((TextBox)WizardCreateUser.CreateUserStep.ContentTemplateContainer.FindControl("TextBoxLastName")).Text;

p.Save();
}

 Ok, still one more thing to be done. add these lines to you Web.config . This block of code will come between <system.web>   </system.web> .

<profile enabled="true">
<properties>
<add name="FirstName"/>
<add name="LastName"/>
</properties>
</profile>

 Ok ,now you can view the page in the Browser. Create a new user. To view Users Profile you need to Create a Page. Call this Page "UserPrifile.aspx" .

How will you create UserPrifile page? See our Article Profiling in ASP .NET 2.0.

Visitors/Readers Comments
(for questions please use The Forum)



omerkamal

For VB remove "OnCreatedUser="WizardCreateUser_CreatedUser" " and add the following code in codebehind.

Protected Sub WizardCreateUser_CreatedUser(ByVal sender As Object, ByVal e As System.EventArgs) Handles WizardCreateUser.CreatedUser
Dim p As ProfileCommon
p = CType(ProfileCommon.Create(WizardCreateUser.UserName, True), ProfileCommon)

p.FirstName = CType(WizardCreateUser.CreateUserStep.ContentTemplateContainer.FindControl("TextBoxFirstName"), TextBox).Text

p.LastName = CType(WizardCreateUser.CreateUserStep.ContentTemplateContainer.FindControl("TextBoxLastName"),TextBox).Text

p.Save()

End Sub

30/12/2006 06:25:49 UTC

stevem
great stuff

31/12/2007 08:10:07 UTC

First
thanks so much that is very nice

17/01/2008 02:32:07 UTC

Felipe Lopez
So far I haven't see such GOOD explanation like the one you present.

18/01/2008 08:37:07 UTC

Santosh

this is great idea to write this type of program so that new developer can understand the code.
 

13/09/2008 05:05:31 UTC




Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)