2009-11-08 3 views
2

사용자는 표준 SQLMemberShipProvider로 작성됩니다.Codebehind에서 오류 메시지를 설정하는 방법 (TemplateLayout을 사용하여 CreateUserWizard 컨트롤)?

예외를 비롯하여 MembershipCreationStatus를 잡아 봤는데 코드를 디버깅 할 때 메시지를 설정하는 메서드가 생깁니다. CreateUserWizardControl의 속성을 사용하여 설정하지만 페이지가 렌더링 될 때 기본값이 표시됩니다. 방금 설정 한 (맞춤) 메시지가 아닙니다.

내가 작업하고있는 마법사는 한 걸음 만 내딛었지만 실제로 잘못된 것을 알고 싶습니다. 이 wizardthingie를 행동 시키려고 노력하면서 하루 종일 이상을 보냈습니다.

어떤 도움이나 지침이 필요합니다.

감사합니다, 아담 여기

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

페이지가로드 될 때 오류에 대한 응답으로 오류 메시지 속성을 설정하고 일부 초기화 프로세스의 일부로 설정하지 않는 이유가 있습니까? – pmarflee

답변

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는 훨씬 더 우아합니다.

관련 문제