注册时向角色添加用户
本文关键字:添加 用户 角色 注册 | 更新日期: 2023-09-27 18:19:06
我使用asp.net应用程序中内置的注册模块来注册新用户。我试图在用户注册时设置角色,但由于某种原因,我无法访问后端代码中的文本框控件以将新用户添加到所选角色,我也试图用此代码找到文本框,但它返回null,这意味着无法找到该控件:
TextBox txtUsername = (TextBox)Page.FindControl("UserName");
Roles.AddUserToRole(txtUsername.Text, "User");
还有,这里是register模块的html标记:
<asp:CreateUserWizardStep ID="RegisterUserWizardStep" runat="server">
<ContentTemplate>
<h2>
Create a New Account
</h2>
<p>
Use the form below to create a new account.
</p>
<p>
Passwords are required to be a minimum of <%= Membership.MinRequiredPasswordLength %> characters in length.
</p>
<span class="failureNotification">
<asp:Literal ID="ErrorMessage" runat="server"></asp:Literal>
</span>
<asp:ValidationSummary ID="RegisterUserValidationSummary" runat="server" CssClass="failureNotification"
ValidationGroup="RegisterUserValidationGroup"/>
<div class="accountInfo">
<fieldset class="register">
<legend>Account Information</legend>
<p>
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
<asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required."
ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
</p>
<p>
<asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-mail:</asp:Label>
<asp:TextBox ID="Email" runat="server" CssClass="textEntry"></asp:TextBox>
<asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email"
CssClass="failureNotification" ErrorMessage="E-mail is required." ToolTip="E-mail is required."
ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
</p>
<p>
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
<asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required."
ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
</p>
<p>
<asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label>
<asp:TextBox ID="ConfirmPassword" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="ConfirmPassword" CssClass="failureNotification" Display="Dynamic"
ErrorMessage="Confirm Password is required." ID="ConfirmPasswordRequired" runat="server"
ToolTip="Confirm Password is required." ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
<asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword"
CssClass="failureNotification" Display="Dynamic" ErrorMessage="The Password and Confirmation Password must match."
ValidationGroup="RegisterUserValidationGroup">*</asp:CompareValidator>
</p>
</fieldset>
<p class="submitButton">
<asp:Button ID="CreateUserButton" runat="server" CommandName="MoveNext" Text="Create User"
ValidationGroup="RegisterUserValidationGroup"/>
</p>
</div>
</ContentTemplate>
<CustomNavigationTemplate>
</CustomNavigationTemplate>
</asp:CreateUserWizardStep>
你知道我做错了什么吗?谢谢,拉齐奥人
这是因为您的UserName
文本框不是页面的直接子框。它嵌套在许多控件中。下面是递归地搜索页面中的所有控件以获取UserName
文本框实例的方法:
private TextBox getUserNameTextBox(ControlCollection ctls)
{
foreach (Control c in ctls)
{
if (c is System.Web.UI.WebControls.TextBox)
{
if (c.ID == "UserName")
return c;
}
if (c.HasControls())
{
getAllCtl(c.Controls);
}
}
return null;
}
YourUserNameTextBox = getuserNameTextBox(Page.Controls);