将角色添加到’CreateUserWizard’

嗨(我对此很新),

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

我有以下代码:

    
Create a New Account
Username: *
Password: *
Confirm Password: *
E-mail: *
Security Question: *
Security Answer: *
Complete
  Your account has been successfully created
     

谢谢。

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

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

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

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

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

在CreateUserWizard的最后一个文本框下添加下拉列表,如下面的代码所示。 ASP:

  <%---------existing items------------%>   Security Answer:   *   <%---------role Dropdownlist to be inserted here------------%> Role    

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

  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