我有一个CreateUserWizard,供用户注册新帐户。
我添加了文本字段和必填字段验证器
然而,当我点击带有空用户名的注册按钮时,我收到错误:
异常详细信息:System.ArgumentException:成员的用户名必须与emptry字符串不同
参数名称:loginName
用户名字段有其requiredfieldvalidator,因此它应该有效。我还添加了
UserNameRequiredErrorMessage
向导的属性,但它没有什么区别。
上面的错误消息中提到的loginName 是LoginView的控件。我在同一页面上有这个,但在注册CreateUserWizard时没有使用它。
为什么会这样?
我的代码:
<asp:Panel ID="pnlRegister" runat="server">
<asp:CreateUserWizard ID="cuwRegister" RequireEmail="false"
OnContinueButtonClick="cuwRegister_ContinueButtonClick"
OnCreatedUser="cuwRegister_CreatedUser"
runat="server">
<WizardSteps>
<asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
<ContentTemplate>
<asp:ValidationSummary ID="uxValSummaryRegister" DisplayMode="BulletList" runat="server" ValidationGroup="CreateUserWizard" ForeColor="#FF5555" />
<asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
<div class="row">
<label>Email</label>
<asp:TextBox ID="UserName" runat="server" />
<asp:requiredfieldvalidator runat="server"
controltovalidate="UserName"
tooltip="Email is a required"
id="reqEmail"
validationgroup="CreateUserWizard"
errormessage="Email is a required" Display="Dynamic">
*
</asp:requiredfieldvalidator>
<asp:RegularExpressionValidator ID="revEmail" runat="server"
ControlToValidate="UserName"
ErrorMessage="Enter correct email address"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*
</asp:RegularExpressionValidator>
</div>
<div class="row">
<label><umbraco:Macro Item="Confirm email" Alias="Dictionary-Item" runat="server" /></label>
<asp:TextBox ID="ConfirmEmail" runat="server" />
<asp:requiredfieldvalidator runat="server"
controltovalidate="ConfirmEmail"
tooltip="Confirm Email address is a required"
id="reqConfirmEmail"
validationgroup="CreateUserWizard"
errormessage="Confirm Email address is a required">
*
</asp:requiredfieldvalidator>
<asp:CompareValidator runat="server"
id="cmpNumbers"
controltovalidate="ConfirmEmail"
controltocompare="UserName"
operator="Equal"
type="String"
errormessage="Email and Confirm Email do not match" />
</div>
<div class="row">
<label><umbraco:Macro Item="Password" Alias="Dictionary-Item" runat="server" /></label>
<asp:TextBox ID="Password" runat="server" TextMode="Password" />
<asp:requiredfieldvalidator runat="server"
controltovalidate="Password"
tooltip="Password is a required"
id="reqPassword"
validationgroup="CreateUserWizard"
errormessage="Password is a required">
*
</asp:requiredfieldvalidator>
</div>
<div class="row">
<label><umbraco:Macro Item="Confirm password" Alias="Dictionary-Item" runat="server" /></label>
<asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password" />
<asp:requiredfieldvalidator runat="server"
controltovalidate="ConfirmPassword"
tooltip="Confirm Password is a required"
id="Requiredfieldvalidator1"
validationgroup="CreateUserWizard"
errormessage="Confirm Password is a required">
*
</asp:requiredfieldvalidator>
<asp:CompareValidator runat="server"
id="CompareValidator1"
controltovalidate="ConfirmPassword"
controltocompare="Password"
operator="Equal"
type="String"
errormessage="Password and Confirm Password do not match" />
</div>
<div class="row">
<label><umbraco:Macro Item="First name" Alias="Dictionary-Item" runat="server" /></label>
<asp:TextBox ID="txtFirstName" runat="server" />
</div>
<div class="row">
<label><umbraco:Macro Item="Surname" Alias="Dictionary-Item" runat="server" /></label>
<asp:TextBox ID="txtSurname" runat="server" />
</div>
<div class="row">
<label><umbraco:Macro Item="Date of birth" Alias="Dictionary-Item" runat="server" /></label>
<asp:TextBox ID="DOB" runat="server" />
<asp:requiredfieldvalidator runat="server"
controltovalidate="DOB"
tooltip="Date of birth is a required"
id="Requiredfieldvalidator2"
validationgroup="CreateUserWizard"
errormessage="Date of birth is a required">
*
</asp:requiredfieldvalidator>
</div>
</ContentTemplate>
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server"></asp:CompleteWizardStep>
</WizardSteps>
并在后面的代码中:
protected void cuwRegister_CreatedUser(object sender, EventArgs e)
{
string email = cuwRegister.UserName;
string username = cuwRegister.UserName;
string password = cuwRegister.Password;
Page.Validate("CreateUserWizard");
if(Page.IsValid)
CreateUser(email, username, password);
}
protected void Page_Load(object sender, EventArgs e)
{
btnRegister.Text = cuwRegister.CreateUserButtonText = umbraco.library.GetDictionaryItem("Register");
cuwRegister.DuplicateUserNameErrorMessage = umbraco.library.GetDictionaryItem("Duplicated email");
cuwRegister.DuplicateEmailErrorMessage = umbraco.library.GetDictionaryItem("Duplicated email");
cuwRegister.InvalidEmailErrorMessage = umbraco.library.GetDictionaryItem("Email address incorrect");
cuwRegister.InvalidPasswordErrorMessage = umbraco.library.GetDictionaryItem("Password incorrect");
cuwRegister.EmailRequiredErrorMessage = umbraco.library.GetDictionaryItem("Email required");
cuwRegister.PasswordRequiredErrorMessage = umbraco.library.GetDictionaryItem("Password required");
cuwRegister.ConfirmPasswordCompareErrorMessage = umbraco.library.GetDictionaryItem("Password not matching");
cuwRegister.UserNameRequiredErrorMessage = umbraco.library.GetDictionaryItem("Password not matching");
Login lwPanelLogin = (Login)lwLogin.FindControl("lwLoginPanel");
if (lwPanelLogin != null)
{
lwPanelLogin.TitleText = umbraco.library.GetDictionaryItem("Already registered? Log in here");
lwPanelLogin.LoginButtonText = umbraco.library.GetDictionaryItem("Login");
lwPanelLogin.RememberMeText = umbraco.library.GetDictionaryItem("Remember me next time");
lwPanelLogin.PasswordLabelText = umbraco.library.GetDictionaryItem("Password");
lwPanelLogin.UserNameLabelText = "Email";
}
}
protected void CreateUser(string email, string username, string password)
{
bool isLoggedIn = System.Web.HttpContext.Current.User.Identity.IsAuthenticated;
if (Membership.GetUserNameByEmail(email) == null)
{
MembershipUser member = Membership.CreateUser("aaa", password, email);
}
if(!isLoggedIn)
if (System.Web.Security.Membership.ValidateUser(username, password))
{
FormsAuthentication.SetAuthCookie(username, true);
}
}
(请注意,我使用用户名字段来获取电子邮件,然后使用电子邮件登录)
答案 0 :(得分:0)
您似乎在“OnCreatedUser”方法中设置了用户名。 向导试图创建该用户后,将触发此操作。 您需要在“OnCreatingUser”方法中设置您的用户名,该方法是事先触发的。
快速取胜,试试这个:
...
<asp:CreateUserWizard ID="cuwRegister" RequireEmail="false" OnContinueButtonClick="cuwRegister_ContinueButtonClick" OnCreatingUser="cuwRegister_CreatedUser" runat="server">
<WizardSteps>
...