Adding roles to the 'CreateUserWizard'

前端 未结 3 549
星月不相逢
星月不相逢 2021-01-12 19:35

Hi (I\'m pretty new to this),

Is it possible to add roles to the \'CreateUserWizard\' tool so that you tick boxes (or view roles in a drop down menu) and once one or

相关标签:
3条回答
  • 2021-01-12 19:54

    Add the dropdownlist under the last textbox of the CreateUserWizard as the following code shows. ASP:

                     <%---------existing items------------%>
                       <tr>
                            <td align="right">
                                <asp:Label runat="server" AssociatedControlID="Answer" ID="AnswerLabel">Security Answer:</asp:Label></td>
                            <td>
                                <asp:TextBox runat="server" ID="Answer"></asp:TextBox>
                                <asp:RequiredFieldValidator runat="server" ControlToValidate="Answer" ErrorMessage="Security answer is required." ValidationGroup="CreateUserWizard1" ToolTip="Security answer is required." ID="AnswerRequired">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
    
                        <%---------role Dropdownlist to be inserted here------------%>
                        <tr><td align="right"><asp:Label runat="server"  ID="RoleLabel">Role</asp:Label></td>
                            <td>
                                <asp:DropDownList ID="RoleDropDownList" runat="server"></asp:DropDownList></td>
    
                        </tr>
    

    Also you have to add in behind code the following:

         protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {  
            ((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("RoleDropDownList")).DataSource = Roles.GetAllRoles(); 
            ((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("RoleDropDownList")).DataBind( );  
            int rolecounter = ((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("RoleDropDownList")).Items.Count; 
            ((DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("RoleDropDownList")).Items[rolecounter - rolecounter].Selected = true;     
        }
    }
    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {
        DropDownList roleDropDownList = (DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("RoleDropDownList");
        Roles.AddUserToRole(CreateUserWizard1.UserName, roleDropDownList.SelectedValue);
    }
    

    This is working for me.Check it

    see for more there http://p2p.wrox.com/asp-net-2-0-basics/42303-createuserwizard-dropdownlist.html

    0 讨论(0)
  • 2021-01-12 20:06

    Briefly, what you need to do is to add the roles dropdown/checklist control somewhere in the <asp:CreateUserWizardStep><ContentTemplate> section.

    Since the control is in a Template, you need to use the following code to find the control in the code behind:

    roleDropDownList = (DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl( "RoleDropDownList" );
    

    You'd bind it like you normally would bind a control in the Page_Init.

    To add the user to the role, use the CreatedUser event of the CreateUserWizard control.

    protected void CreateUserWizard1_CreatedUser( object sender, EventArgs e )
    {
      Roles.AddUserToRole( CreateUserWizard1.UserName, roleDropDownList.SelectedValue );
    }
    
    0 讨论(0)
  • 2021-01-12 20:18

    if you want to assign the role to the user you just created you can use this :

    Roles.AddUserToRole((sender as CreateUserWizard).UserName, "YourRole");
    
    0 讨论(0)
提交回复
热议问题