2

ユーザーは、標準の SQLMemberShipProvider で作成されます。

MembershipCreationStatus と例外をキャッチしようとしましたが、コードをデバッグすると、メッセージを設定するメソッドにたどり着きました。CreateUserWizardControl のプロパティを使用してそれらを設定しましたが、ページがレンダリングされるとデフォルト値が表示されます。私が設定した(カスタム)メッセージではありません。

私が取り組んでいるウィザードには 1 つのステップしかありませんが、何が間違っているのかを本当に知りたいです。私は、この魔法使いを動作させるために 1 日以上を費やしました。

ヘルプやガイダンスを探しています。

ありがとう、アダム

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

2 に答える 2

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 コントロール内にはありませんが、ユーザーにはわかりません。不器用ですが、うまくいきます。ASP.NET MVC ははるかにエレガントです。

于 2011-09-21T14:48:21.140 に答える
0

静的文字列のみを使用している場合、エラーが発生したときに値を変更するのはなぜですか?

プロパティは、コントロールのプロパティ パネルで公開されます。そこで値を設定すると、すべてが機能するはずです。

于 2010-09-08T20:36:44.167 に答える