将角色添加到“CreateUserWizard”

发布于 2024-08-10 07:30:53 字数 9387 浏览 9 评论 0原文

您好(我对此很陌生),

是否可以将角色添加到“CreateUserWizard”工具中,以便您勾选框(或在下拉菜单中查看角色),并且一旦选择了一个或多个角色,此信息就会显示自动添加到asp.net配置中?

我有以下代码:

<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" BackColor="#F7F6F3" BorderColor="#E6E2D8" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" Height="260px" Width="568px">
        <WizardSteps>
            <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
                <ContentTemplate>
                    <table border="0" style="font-size: 100%; width: 568px; font-family: Verdana; height: 260px">
                        <tr>
                            <td align="center" colspan="2" style="font-weight: bold; color: white; background-color: #5d7b9d">
                                Create a New Account</td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:</asp:Label></td>
                            <td>
                                <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="CreateUserWizard1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label></td>
                            <td>
                                <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="CreateUserWizard1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label></td>
                            <td>
                                <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="CreateUserWizard1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-mail:</asp:Label></td>
                            <td>
                                <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="CreateUserWizard1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="QuestionLabel" runat="server" AssociatedControlID="Question">Security Question:</asp:Label></td>
                            <td>
                                <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="CreateUserWizard1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="AnswerLabel" runat="server" AssociatedControlID="Answer">Security Answer:</asp:Label></td>
                            <td>
                                <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="CreateUserWizard1">*</asp:RequiredFieldValidator>
                            </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="CreateUserWizard1"></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>
                    </table>
                </ContentTemplate>
            </asp:CreateUserWizardStep>
            <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
                <ContentTemplate>
                    <table border="0" style="font-size: 100%; width: 568px; font-family: Verdana; height: 260px">
                        <tr>
                            <td align="center" colspan="2" style="font-weight: bold; color: white; background-color: #5d7b9d">
                                Complete</td>
                        </tr>
                        <tr>
                            <td style="text-align: left">
                                &nbsp; Your account has been successfully created</td>
                        </tr>
                        <tr>
                            <td align="right" colspan="2" style="text-align: right">
                                <asp:Button ID="ContinueButton" runat="server" BackColor="#FFFBFF" BorderColor="#CCCCCC"
                                    BorderStyle="Solid" BorderWidth="1px" CausesValidation="False" CommandName="Continue"
                                    Font-Names="Verdana" ForeColor="#284775" PostBackUrl="~/Default.aspx" Text="Continue"
                                    ValidationGroup="CreateUserWizard1" Width="105px" />
                                &nbsp; &nbsp; &nbsp;</td>
                        </tr>
                    </table>
                </ContentTemplate>
            </asp:CompleteWizardStep>
        </WizardSteps>
        <SideBarStyle BackColor="#5D7B9D" BorderWidth="0px" Font-Size="0.9em" VerticalAlign="Top" />
        <SideBarButtonStyle BorderWidth="0px" Font-Names="Verdana" ForeColor="White" />
        <ContinueButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid"
            BorderWidth="1px" Font-Names="Verdana" ForeColor="#284775" />
        <NavigationButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid"
            BorderWidth="1px" Font-Names="Verdana" ForeColor="#284775" />
        <HeaderStyle BackColor="#5D7B9D" BorderStyle="Solid" Font-Bold="True" Font-Size="0.9em"
            ForeColor="White" HorizontalAlign="Center" />
        <CreateUserButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid"
            BorderWidth="1px" Font-Names="Verdana" ForeColor="#284775" />
        <TitleTextStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <StepStyle BorderWidth="0px" />
    </asp:CreateUserWizard>

谢谢。

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 more have been chosen, this information is added to the asp.net configuration automatically?

I have the following code:

<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" BackColor="#F7F6F3" BorderColor="#E6E2D8" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" Height="260px" Width="568px">
        <WizardSteps>
            <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
                <ContentTemplate>
                    <table border="0" style="font-size: 100%; width: 568px; font-family: Verdana; height: 260px">
                        <tr>
                            <td align="center" colspan="2" style="font-weight: bold; color: white; background-color: #5d7b9d">
                                Create a New Account</td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:</asp:Label></td>
                            <td>
                                <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="CreateUserWizard1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label></td>
                            <td>
                                <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="CreateUserWizard1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label></td>
                            <td>
                                <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="CreateUserWizard1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-mail:</asp:Label></td>
                            <td>
                                <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="CreateUserWizard1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="QuestionLabel" runat="server" AssociatedControlID="Question">Security Question:</asp:Label></td>
                            <td>
                                <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="CreateUserWizard1">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="AnswerLabel" runat="server" AssociatedControlID="Answer">Security Answer:</asp:Label></td>
                            <td>
                                <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="CreateUserWizard1">*</asp:RequiredFieldValidator>
                            </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="CreateUserWizard1"></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>
                    </table>
                </ContentTemplate>
            </asp:CreateUserWizardStep>
            <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
                <ContentTemplate>
                    <table border="0" style="font-size: 100%; width: 568px; font-family: Verdana; height: 260px">
                        <tr>
                            <td align="center" colspan="2" style="font-weight: bold; color: white; background-color: #5d7b9d">
                                Complete</td>
                        </tr>
                        <tr>
                            <td style="text-align: left">
                                  Your account has been successfully created</td>
                        </tr>
                        <tr>
                            <td align="right" colspan="2" style="text-align: right">
                                <asp:Button ID="ContinueButton" runat="server" BackColor="#FFFBFF" BorderColor="#CCCCCC"
                                    BorderStyle="Solid" BorderWidth="1px" CausesValidation="False" CommandName="Continue"
                                    Font-Names="Verdana" ForeColor="#284775" PostBackUrl="~/Default.aspx" Text="Continue"
                                    ValidationGroup="CreateUserWizard1" Width="105px" />
                                     </td>
                        </tr>
                    </table>
                </ContentTemplate>
            </asp:CompleteWizardStep>
        </WizardSteps>
        <SideBarStyle BackColor="#5D7B9D" BorderWidth="0px" Font-Size="0.9em" VerticalAlign="Top" />
        <SideBarButtonStyle BorderWidth="0px" Font-Names="Verdana" ForeColor="White" />
        <ContinueButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid"
            BorderWidth="1px" Font-Names="Verdana" ForeColor="#284775" />
        <NavigationButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid"
            BorderWidth="1px" Font-Names="Verdana" ForeColor="#284775" />
        <HeaderStyle BackColor="#5D7B9D" BorderStyle="Solid" Font-Bold="True" Font-Size="0.9em"
            ForeColor="White" HorizontalAlign="Center" />
        <CreateUserButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid"
            BorderWidth="1px" Font-Names="Verdana" ForeColor="#284775" />
        <TitleTextStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <StepStyle BorderWidth="0px" />
    </asp:CreateUserWizard>

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

豆芽 2024-08-17 07:30:53

简而言之,您需要做的就是在 部分中的某处添加角色下拉列表/清单控件。

由于该控件位于模板中,因此您需要使用以下代码在后面的代码中查找该控件:

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

您可以像通常在 Page_Init 中绑定控件一样绑定它。

要将用户添加到角色,请使用 CreateUserWizard 控件的 CreatedUser 事件。

protected void CreateUserWizard1_CreatedUser( object sender, EventArgs e )
{
  Roles.AddUserToRole( CreateUserWizard1.UserName, roleDropDownList.SelectedValue );
}

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 );
}
情深已缘浅 2024-08-17 07:30:53

如果您想将角色分配给刚刚创建的用户,可以使用以下命令:

Roles.AddUserToRole((sender as CreateUserWizard).UserName, "YourRole");

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

Roles.AddUserToRole((sender as CreateUserWizard).UserName, "YourRole");
岛徒 2024-08-17 07:30:53

在 CreateUserWizard 的最后一个文本框下添加下拉列表,如以下代码所示。 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>

您还必须在后面的代码中添加以下内容:

     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);
}

这对我有用。检查一下

,了解更多信息 http://p2p.wrox.com/asp-net-2-0-basics/42303-createuserwizard-dropdownlist.html

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文