English 中文(简体)
• 如何从密码中设定错误信息(CreateUserWizard 控制,使用TemplateLayout)?
原标题:How to set error messages from codebehind (CreateUserWizard control, using TemplateLayout)?

用户是用一个标准的标准成员供应商创建的。

我试图追捕会员资格和例外,在我辩论守则时,我确实掌握了信息的方法。 我把这些财产放在创建UserWizardControl上,但当该页落空时,就显示出了缺省值,而不是我刚才确定的(tom)电文。

我的辛勤工作只是一个步骤,但我真的想知道我做什么错了。 我花了一天多时间,试图让这一令人izar笑.的行为发生。

寻找某种帮助或指导。

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 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.
   ...
  }
 }
问题回答

ASP.NET 有时网络论坛如此艰难。 这里的解决方案是一米。

第1步:在网页上添加另一个标签控制:

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

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

当出现错误时,你会看到一种爱心的错误信息。 它是登记册UserWizard控制范围内的一个环节,但用户获得的笔数能够说明。 Kludgey, 但它运作。 ASP. NET MVC远比以往更远。

如果你只是使用静态扼杀,那么在出现错误时,你为什么会改变价值?

如果你确定所有应发挥作用的价值,这些财产就暴露在财产小组控制之下。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签