开发者

How to set error messages from codebehind (CreateUserWizard control, using TemplateLayout)?

Users are created with a standard SQLMemberShipProvider.

I've tried catching MembershipCreationStatus as well as exceptions, and when I debug the code I do get to the method that sets the messages. I set them using the properties on the CreateUserWizardControl, but when the page is rendered the default values are displayed; not the (custom) messages I just set.

The wizard I'm working on has just one step but I really would like to know what I'm doing wrong. I've spent more than a whole day on trying to get this wizardthingie to behave.

Looking for some help or guidance.

Thanks, Adam

<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 cla开发者_StackOverflow社区ss="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>

Here's the codebehind (sections of interest):

 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.
   ...
  }
 }


ASP.NET WebForms suck so hard sometimes. Here's the solution I'm using for that.

Step 1: Add another Label control to the top of the page:

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

Note the CssClass.

Step 2: When an error occurs, put the error message in that label.

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

When an error occurs, you see a lovely formatted error message. It isn't inside the RegisterUserWizard control, but the user won't be able to tell. Kludgey, but it works. ASP.NET MVC is much more elegant.


Why are you changing the value when en error occurs if you are only using static strings?

The properties are exposed on the properties panel for the control, if you set the values there everything should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜