如何从后面的代码设置错误消息(CreateUserWizard控件,使用模板布局)?

时间:2009-11-08 18:46:37

标签: asp.net createuserwizard

使用标准SQLMemberShipProvider创建用户。

我尝试过捕获MembershipCreationStatus以及异常,当我调试代码时,我确实得到了设置消息的方法。我使用CreateUserWizardControl上的属性设置它们,但是当呈现页面时,显示默认值;不是我刚设置的(自定义)消息。

我正在研究的向导只有一步,但我真的想知道我做错了什么。我花了一天多的时间试图让这个巫师表现出来。

寻求一些帮助或指导。

谢谢, 亚当

<asp:CreateUserWizard ID="createUserWizard" runat="server" 
  ContinueDestinationPageUrl="/user/profile/" 
  CreateUserButtonText="Continue" RequireEmail="False" ActiveStepIndex="0">
  <WizardSteps>
   <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server" Title="Account details">
    <ContentTemplate>
     <fieldset>
      <legend>Account details</legend>

      <div>
       <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Desired username:</asp:Label>
       <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
      </div>
      <div>
       <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Desired password:</asp:Label>
       <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
       <asp:RequiredFieldValidator ID="PasswordRequiredValidator" runat="server" ControlToValidate="Password" 
        Display="Dynamic" EnableClientScript="false" ValidationGroup="createUserWizard">*</asp:RequiredFieldValidator>
      </div>
      <div>
       <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Confirm password:</asp:Label>
       <asp:TextBox runat="server" ID="ConfirmPassword" TextMode="Password"></asp:TextBox>
      </div>
     </fieldset>

     <div class="errors">
      <asp:ValidationSummary runat="server" ID="validationSummary" 
       DisplayMode="BulletList" ValidationGroup="createUserWizard" 
       HeaderText="<b>Please correct the following fields:</b>" 
       ShowMessageBox="False" ShowSummary="true" />

      <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="false" />
     </div>
    </ContentTemplate>
   </asp:CreateUserWizardStep>
  </WizardSteps>
 </asp:CreateUserWizard>

这是代码隐藏(感兴趣的部分):

 protected override void OnInit(EventArgs e)
 {
  base.OnInit(e);

  createUserWizard.CreatedUser += OnUserCreated;
  createUserWizard.CreateUserError += OnError;
  createUserWizard.NextButtonClick += OnNextButtonClick;
 }

 protected void OnUserCreated(object sender, EventArgs e)
 {
  //Add user to group and redirect to destination page.
 }

 private void OnNextButtonClick(object sender, WizardNavigationEventArgs e)
 {
  CreateUserWizard wizard = sender as CreateUserWizard;

  if (wizard.ActiveStepIndex == 0)
  {
   try 
   {
    if (Membership.ValidateUser(createUserWizard.UserName, createUserWizard.Password))
    {
     MembershipUser newUser = Membership.CreateUser(
      createUserWizard.UserName,
      createUserWizard.Password);

     //add user to group and redirect to destination page
    }
   }
   catch (MembershipCreateUserException ex)
   {
    SetFailureMessage(ex.StatusCode);
    e.Cancel = true;
   }
  }
 }

 public void SetFailureMessage(MembershipCreateStatus status)
 {
  switch (status)
  {
   case MembershipCreateStatus.DuplicateUserName:
    createUserWizard.DuplicateUserNameErrorMessage = "Username is not available."; //the string is fetched from a database.
    break;

   case MembershipCreateStatus.InvalidPassword:
    createUserWizard.InvalidPasswordErrorMessage = "Password is not secure enough"; //the string is fetched from a database.
   ...
  }
 }

 private void OnError(object sender, CreateUserErrorEventArgs e)
 {
  if (e.CreateUserError == MembershipCreateStatus.InvalidPassword)
  {
   CreateUserWizard wizard = sender as CreateUserWizard;
   wizard.InvalidPasswordErrorMessage = "Password is not secure enough"; //the string is fetched from a database.
   ...
  }
 }

2 个答案:

答案 0 :(得分:2)

ASP.NET WebForms有时会非常糟糕。这是我正在使用的解决方案。

步骤1:将另一个Label控件添加到页面顶部:

<asp:Label ID="lblErrorMessage" Text="" runat=server CssClass="failureNotification"></asp:Label>

注意CssClass。

步骤2:发生错误时,将错误消息放入该标签中。

try
{
    //Do stuff here
}
catch (Exception ex)
{
    lblErrorMessage.Text = string.Format("An error has occurred. Please tell tech support that you saw this: <br />{0}: {1}", ex.GetType(), ex.Message);
    e.Cancel = true;
}

发生错误时,您会看到一个可爱的格式化错误消息。它不在RegisterUserWizard控件内,但用户将无法分辨。 Kludgey,但它确实有效。 ASP.NET MVC更优雅。

答案 1 :(得分:0)

如果您只使用静态字符串,为什么在发生错误时更改值?

属性在控件的属性面板上公开,如果在那里设置值,一切都应该有效。