除了Enter键之外,asp.Net ValidationGroup正确验证

时间:2013-03-12 19:33:50

标签: asp.net validationgroup

我在这里看到的各种问题与这个问题非常接近,但似乎没有一个问题符合这一特定情况。

描述: 我在主页面中有ID和密码字段以及登录按钮。这些都在“LoginValidationGroup”中,它们工作正常。

现在,在Default.aspx中,我有一个电子邮件TextBox和提交按钮,它们位于“NotifyMeValidation”组中,它们也可以工作,但是如果你按下回车键而不是提交按钮则不行。提交按钮工作正常 - 输入密钥......不是那么多。

Enter键导致验证在LoginValidationGroup上发生,即使TextBox设置为CausesValidation =“true”,并且它位于NotifyMeValidation组中。

我保证人们会在该框中输入电子邮件并按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" />

2 个答案:

答案 0 :(得分:3)

尝试在每组控件周围添加<asp:Panel>,并在用户点击 Enter 时将DefaultButton property设置为您要点击的按钮的ID。

答案 1 :(得分:1)

只是为了解释这里发生的事情,当您在文本框中按Enter键时,将触发aspx的默认按钮。要处理这个问题,你可以在文本框上单击回车键时触发一些jQuery代码,然后这段代码将执行你需要的特定按钮的点击。 Check this