2013-03-12 60 views
0

我在這裏看到了很多與這個問題非常接近的問題,但沒有一個看起來適合這個特定場景。asp.Net ValidationGroup驗證正確,除了輸入密鑰

說明: 我在Master頁面中有ID和密碼字段,帶有Log On按鈕。這些在「LoginValidationGroup」中,它們工作正常。

現在,在Default.aspx中,我有一個電子郵件文本框和提交按鈕,它們在「NotifyMeValidation」組中,並且它們也可以工作,但是如果按Enter鍵而不是按下提交按鈕,則不是。 「提交」按鈕正常工作 - 輸入密鑰...並非如此。

即使TextBox設置爲CausesValidation =「true」,並且它位於NotifyMeValidation組中,Enter鍵仍會在LoginValidationGroup上進行驗證。

我保證人們會在該框中輸入一個電子郵件並按Enter鍵。我會!!!當他們這樣做時,他們會在頂部看到一個標註氣球,告訴他們用戶ID是必需的。

這是什麼錯誤?如果我需要發佈實際的代碼,我可以這樣做。

其實,讓我繼續做下去。

從Default.aspx的:

<asp:RequiredFieldValidator 
    runat="server" 
    ValidationGroup="NotifyMeValidation" 
    ID="EmailRequiredValidator" 
    ControlToValidate="SenderEmailTextBox" 
    ErrorMessage="Email is Required" 
    Display="None" /> 
<ajaxToolkit:ValidatorCalloutExtender 
    runat="Server" 
    PopupPosition="Left" 
    ID="EmailRequiredValidatorExtender" 
    Width="185px" 
    TargetControlID="EmailRequiredValidator" 
    WarningIconImageUrl="/Images/warning.gif" /> 
<asp:Label ID="SenderEmailLabel" runat="server" ssociatedControlID="SenderEmailTextBox" EnableViewState="false" Text="Email:"></asp:Label>&nbsp;&nbsp; 
<asp:TextBox ID="SenderEmailTextBox" runat="server" ValidationGroup="NotifyMeValidation" Width="350" BackColor="#cccccc" CausesValidation="true"></asp:TextBox> 
<br /><br /> 
<asp:Button ID="SubmitButton" runat="server" ValidationGroup="NotifyMeValidation" EnableViewState="false" CssClass="submit" Text="Send" CausesValidation="true" OnClick="btnSubmit_Click" /> 

從母版頁:

<asp:Label CssClass="LoginHeading" ID="UserNameLabel" runat="server" AssociatedControlID="UserNameTextbox">UserName:&nbsp;</asp:Label> 
<asp:TextBox CssClass="LoginTextBox" ID="UserNameTextbox" runat="server" ValidationGroup="LoginValidation" CausesValidation="True"></asp:TextBox>&nbsp; 
<asp:Label CssClass="LoginHeading" ID="PasswordLabel" runat="server" AssociatedControlID="PasswordTextbox">Password:&nbsp;</asp:Label> 
<asp:TextBox CssClass="LoginTextBox" ID="PasswordTextBox" runat="server" TextMode="Password" ValidationGroup="LoginValidation" CausesValidation="True"></asp:TextBox>&nbsp; 
<asp:Button CssClass="LoginHeading" ID="LoginButton" runat="server" Text="Button" CommandName="Login" ValidationGroup="LoginValidation" CausesValidation="True" onclick="LoginButton_Click" /> 

和法師頁面驗證...

<asp:RequiredFieldValidator runat="server" ID="UIDRequired" 
    ValidationGroup="LoginValidation" 
    ControlToValidate="UserNameTextbox" 
    Display="None" 
    ErrorMessage="<b>Required Field Missing</b><br />A User ID is required." /> 
<ajaxToolkit:ValidatorCalloutExtender 
    runat="Server" 
    ID="UIDValidationExtender" 
    PopupPosition="Left" 
    Width="185px" 
    TargetControlID="UIDRequired" 
    WarningIconImageUrl="/Images/warning.gif" /> 
<asp:RequiredFieldValidator runat="server" ID="PWRequired" 
    ValidationGroup="LoginValidation" 
    ControlToValidate="PasswordTextbox" 
    Display="None" 
    ErrorMessage="<b>Required Field Missing</b><br />A password is required." /> 
<ajaxToolkit:ValidatorCalloutExtender 
    runat="Server" 
    ID="PWValidationExtender" 
    PopupPosition="Left" 
    TargetControlID="PWRequired" 
    Width="185px" 
    WarningIconImageUrl="/Images/warning.gif" /> 
<asp:CustomValidator 
    runat="server" 
    Display="None" 
    ValidationGroup="LoginValidation" 
    ID="CustomUserExistsValidator" 
    ControlToValidate="UserNameTextbox" 
    ErrorMessage="<b>Invalid UserID!</b><br />User ID doesn't exist. Please try again.<br />" 
    OnServerValidate="CustomCheckUserExists"/> 
<ajaxToolkit:ValidatorCalloutExtender 
    runat="server" 
    PopupPosition="Left" 
    ID="UserIDCalloutExtender" 
    TargetControlID="CustomUserExistsValidator" 
    Width="185px" 
    WarningIconImageUrl="/Images/warning.gif" /> 
<asp:CustomValidator 
    runat="server" 
    ID="CustomPWValidator" 
    Display="None" 
    ValidationGroup="LoginValidation" 
    ControlToValidate="PasswordTextbox" 
    ErrorMessage="<b>Invalid Password!</b><br />Please try again.<br />" 
    OnServerValidate="CustomValidatePassword"/> 
<ajaxToolkit:ValidatorCalloutExtender 
    runat="server" 
    PopupPosition="Left" 
    ID="PWCalloutExtender" 
    TargetControlID="CustomPWValidator" 
    Width="185px" 
    WarningIconImageUrl="/Images/warning.gif" /> 
<asp:CustomValidator 
    runat="server" 
    ID="CustomUserApprovedValidator" 
    Display="None" 
    ValidationGroup="LoginValidation" 
    ControlToValidate="UserNameTextbox" 
    ErrorMessage="<b>Approval Error!</b><br />This UserID exists, but is not yet approved.<br />" 
    OnServerValidate="CustomCheckUserApproved"/> 
<ajaxToolkit:ValidatorCalloutExtender 
    runat="server" 
    PopupPosition="Left" 
    ID="UserApprovedCalloutExtender" 
    TargetControlID="CustomUserApprovedValidator" 
    Width="185px" 
    WarningIconImageUrl="/Images/warning.gif" /> 

回答

3

嘗試增加周圍的每一個<asp:Panel>設置控件並將DefaultButton property設置爲您想在t時點擊的按鈕的ID他用戶點擊輸入

+0

甜!理查德,工作很好,而且實施起來非常簡單!感謝您的提示! – DJGray 2013-03-12 20:06:23

+0

+1這比我的解決方案好得多。 – 2013-03-12 20:14:12

1

爲了解釋這裏發生了什麼,當您按下文本框上的回車鍵時,aspx的默認按鈕被觸發。爲了處理這個問題,你可以在文本框中點擊回車鍵時觸發一些jQuery代碼,然後這段代碼將執行你所需要的特定按鈕的點擊。 Check this