2009-11-08 24 views
2

用戶使用標準的SQLMemberShipProvider創建。如何從代碼隱藏(CreateUserWizard控件,使用TemplateLayout)設置錯誤消息?

我已經嘗試捕獲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. 
    ... 
    } 
} 
+0

有沒有爲什麼你設置錯誤消息屬性來響應錯誤,而不是作爲頁面加載時的一些初始化過程的一部分? – pmarflee

回答

0

你爲什麼要改變值時,恩,如果你只使用靜態字符串出現錯誤?

屬性顯示在控件的屬性面板上,如果你設置了值,那麼一切都應該工作。

2

有時ASP.NET WebForms很難吸收。這是我爲此使用的解決方案。

步驟1:將另一個標籤控件添加到頁面頂部:

<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更優雅。