自定义CreateUserWizard控件

这两天做一个小项目,需要把用户在注册的时候填入的个人信息保存到Profile中,自然而然的想到要自定义CreateUserWizard控件的创建用户步骤(CreateUserWizardStep),现在把我的实现过程写下来,跟大家探讨一下。
     一 . 在web.config中定义Profile字段,设置各字段的allowAnonymous="true"。
          <profile enabled="true">
                 <properties>
                    <group name="address">
                        <add name="Name" allowAnonymous="true"/>
                        <add name="Address" allowAnonymous="true"/>
                        <add name="Province" allowAnonymous="true"/>
                        <add name="City" allowAnonymous="true"/>
                        <add name="PostCode" allowAnonymous="true"/>
                        <add name="Telephone" allowAnonymous="true"/>
                    </group>
                 </properties>
            </profile>
     二. 建一个web窗体,拖入一个CreateUserWizard,自定义其创建用户步骤,创建用户后,此用户会自动登录LoginCreatedUser="true" 
,代码如下:
     <asp:CreateUserWizard
        id="CreateUserWizard1"
         CompleteSuccessText="完成您的注册!"    
         LoginCreatedUser="true" 
          Runat="server" OnCreatedUser="CreateUserWizard1_CreatedUser">
       
         <WizardSteps>
             <asp:CreateUserWizardStep runat="server">
                 <ContentTemplate>
                     <table border="0">
                         <tr>
                             <td align="center" colspan="2">
                                 注册新帐户</td>
                         </tr>
                         <tr>
                             <td align="right">
                                 <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">用户名:</asp:Label></td>
                             <td>
                                 <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                                 <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
                                     ErrorMessage="必须填写“用户名”。" ToolTip="必须填写“用户名”。" ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                             </td>
                         </tr>
                         <tr>
                             <td align="right">
                                 <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">密码:</asp:Label></td>
                             <td>
                                 <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                                 <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
                                     ErrorMessage="必须填写“密码”。" ToolTip="必须填写“密码”。" ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                             </td>
                         </tr>
                         <tr>
                             <td align="right">
                                 <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">确认密码:</asp:Label></td>
                             <td>
                                 <asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password"></asp:TextBox>
                                 <asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" ControlToValidate="ConfirmPassword"
                                     ErrorMessage="必须填写“确认密码”。" ToolTip="必须填写“确认密码”。" ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                             </td>
                         </tr>
                         <tr>
                             <td align="right">
                                 <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">电子邮件:</asp:Label></td>
                             <td>
                                 <asp:TextBox ID="Email" runat="server"></asp:TextBox>
                                 <asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email"
                                     ErrorMessage="必须填写“电子邮件”。" ToolTip="必须填写“电子邮件”。" ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                             </td>
                         </tr>
                         <tr>
                             <td align="right">
                                 <asp:Label ID="QuestionLabel" runat="server" AssociatedControlID="Question">安全提示问题:</asp:Label></td>
                             <td>
                                 <asp:TextBox ID="Question" runat="server"></asp:TextBox>
                                 <asp:RequiredFieldValidator ID="QuestionRequired" runat="server" ControlToValidate="Question"
                                     ErrorMessage="必须填写“安全提示问题”。" ToolTip="必须填写“安全提示问题”。" ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                             </td>
                         </tr>
                         <tr>
                             <td align="right">
                                 <asp:Label ID="AnswerLabel" runat="server" AssociatedControlID="Answer">安全答案:</asp:Label></td>
                             <td>
                                 <asp:TextBox ID="Answer" runat="server"></asp:TextBox>
                                 <asp:RequiredFieldValidator ID="AnswerRequired" runat="server" ControlToValidate="Answer"
                                     ErrorMessage="必须填写“安全答案”。" ToolTip="必须填写“安全答案”。" ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                             </td>
                         </tr>
                         <tr>
                             <td align="center" colspan="2">
                                 <asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password"
                                     ControlToValidate="ConfirmPassword" Display="Dynamic" ErrorMessage="“密码”和“确认密码”必须匹配。"
                                     ValidationGroup="CreateUserWizard1"></asp:CompareValidator>
                             </td>
                         </tr>
                         
                         <tr>
                         <td align="right">
                             收货人:</td>
                         <td>
                             <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                             
                         </td>
                     </tr>
                     <tr>
                         <td align="right">
                             收货人地址:</td>
                         <td>
                             <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
                         </td>
                     </tr>
                     <tr>
                         <td align="right">
                             省份:</td>
                         <td>
                             <asp:TextBox ID="txtProvince" runat="server"></asp:TextBox>
                         </td>
                     </tr>
                     <tr>
                         <td align="right">
                             城市:</td>
                         <td>
                             <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
                         </td>
                     </tr>
                     <tr>
                         <td align="right">
                             邮编:</td>
                         <td>
                             <asp:TextBox ID="txtPostCode" runat="server"></asp:TextBox>
                         </td>
                     </tr>
                     <tr>
                         <td align="right">
                             电话:</td>
                         <td>
                             <asp:TextBox ID="txtTelephone" runat="server"></asp:TextBox>
                         </td>
                     </tr>
                     <tr>
                             <td align="center" colspan="2" style="color: red">
                                 <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
                             </td>
                         </tr>
                     </table>
                 </ContentTemplate>
             </asp:CreateUserWizardStep>
              <asp:CompleteWizardStep runat="server" Title="">
             </asp:CompleteWizardStep>
         </WizardSteps>
    </asp:CreateUserWizard>
三.在CreatedUser事件中保存个人信息,在此遇到的问题,主要是如何获取 CreateUserStep中的那些TextBox   
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {

        Profile.address.Name = ((TextBox)(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtName"))).Text;
        Profile.address.Address = ((TextBox)(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtAddress"))).Text;
        Profile.address.Province = ((TextBox)(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtProvince"))).Text;
        Profile.address.City = ((TextBox)(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtCity"))).Text;
        Profile.address.PostCode = ((TextBox)(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtPostCode"))).Text;
        Profile.address.Telephone = ((TextBox)(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("txtTelephone"))).Text;
    }
四。在上面CreatedUser事件中保存的是匿名用户的信息,需要在Global.asax文件中设置迁移匿名用户的程序,在此要注意的是 if (!string.IsNullOrEmpty(anoProfile.address.Name)),要确保进行此判断,不然若匿名用户的Profile为空,用户登录后其原先的profile信息就会被覆盖了
      

void Profile_OnMigrateAnonymous(object sender, ProfileMigrateEventArgs e)
    {
        // 获取匿名用户的profile
        ProfileCommon anoProfile = Profile.GetProfile(e.AnonymousID);


         if (!string.IsNullOrEmpty(anoProfile.address.Name))
        {
            Profile.address.Name = anoProfile.address.Name;
            Profile.address.Address = anoProfile.address.Address;
            Profile.address.Province = anoProfile.address.Province;
            Profile.address.City = anoProfile.address.City;
            Profile.address.PostCode = anoProfile.address.PostCode;
            Profile.address.Telephone = anoProfile.address.Telephone;
            Profile.Save();
            
        }
        ProfileManager.DeleteProfile(e.AnonymousID);
        AnonymousIdentificationModule.ClearAnonymousIdentifier();
    }
    备注:上面在CreatedUser事件中保存的是匿名用户的Profile信息,如果在CreateUserWizard1_ContinueButtonClick事件中则保存的是注册用户的Profile信息,这样

posted @ 2011-01-22 10:44  小锋神  阅读(1724)  评论(0编辑  收藏  举报