如何让验证器在ASP.NET中工作?

时间:2011-11-16 20:14:53

标签: c# asp.net validation

我正在尝试在我的ASP.NET Webforms应用程序中实现验证。现在,唯一有效的验证器是出生日期(DOB)字段。这是一个自定义字段。

ASPX代码

<table>

      <tr> <td align="center" colspan="2"> 
      <asp:Label ID="RegisterTitleID" runat="server">Register here for your New Account </asp:Label></td>
      </tr>

      <tr> 
      <td align="right"> <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="EmailTextBox">E-mail:</asp:Label> </td>
      <td> <asp:TextBox ID="EmailTextBox" runat="server" Height="25px" Width="200px" 
              CausesValidation="True"></asp:TextBox></td>
           <asp:RequiredFieldValidator ID="EmailRequired" runat="server" 
            ControlToValidate="EmailTextBox" ErrorMessage="E-mail is required." 
            ToolTip="E-mail is required." ValidationGroup="RegistrationWizard" 
              Display="Dynamic">*</asp:RequiredFieldValidator> </tr>

      <tr> 
      <td align="right"> <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="PasswordTextBox">Password:</asp:Label> </td>
      <td> <asp:TextBox ID="PasswordTextBox" runat="server" TextMode="Password" 
              Height="25px" Width="200px" CausesValidation="True"></asp:TextBox></td> 
           <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" 
            ControlToValidate="PasswordTextBox" ErrorMessage="Password is required." 
            ToolTip="Password is required." ValidationGroup="RegistrationWizard" 
              Display="Dynamic">*</asp:RequiredFieldValidator> </tr>

      <tr>
      <td align="right"> <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPasswordTextBox">Confirm Password:</asp:Label> </td>
      <td> <asp:TextBox ID="ConfirmPasswordTextBox" runat="server" TextMode="Password" Height="25px" Width="200px"></asp:TextBox></td>
           <asp:RequiredFieldValidator ID="ConfirmPasswordRequired" runat="server" 
            ControlToValidate="ConfirmPasswordTextBox" ErrorMessage="Confirm Password is required." 
            ToolTip="Confirm Password is required." ValidationGroup="RegistrationWizard">*</asp:RequiredFieldValidator> </tr>

      <tr> 
      <td align="center" colspan="2"> 
      <asp:CompareValidator ID="PasswordCompare" runat="server" 
            ControlToCompare="PasswordTextBox" ControlToValidate="ConfirmPasswordTextBox" 
            Display="Dynamic" 
            ErrorMessage="The Password and Confirmation Password must match." 
            ValidationGroup="RegistrationWizard"> </asp:CompareValidator> </td> </tr>

      <tr> 
      <td align="right"> <asp:Label ID="TitleLabel" runat="server" AssociatedControlID="TitleTextBox">Title:</asp:Label> </td>
      <td> <asp:TextBox ID="TitleTextBox" runat="server" Height="25px" Width="200px"></asp:TextBox></td>
           <asp:RequiredFieldValidator ID="TitleRequired" runat="server" 
            ControlToValidate="TitleTextBox" ErrorMessage="Title is required." 
            ToolTip="Title is required." ValidationGroup="RegistrationWizard">*</asp:RequiredFieldValidator> </tr>

      <tr> 
      <td align="right"> <asp:Label ID="FirstNameLabel" runat="server" AssociatedControlID="FirstNameTextBox">First Name:</asp:Label> </td>
      <td> <asp:TextBox ID="FirstNameTextBox" runat="server" Height="25px" Width="200px"></asp:TextBox></td>
           <asp:RequiredFieldValidator ID="FirstNameRequired" runat="server" 
            ControlToValidate="FirstNameTextBox" ErrorMessage="First Name is required." 
            ToolTip="First Name is required." ValidationGroup="RegistrationWizard">*</asp:RequiredFieldValidator> </tr>

      <tr> 
      <td align="right"> <asp:Label ID="SurnameLabel" runat="server" AssociatedControlID="SurnameTextBox">Surname:</asp:Label> </td>
      <td> <asp:TextBox ID="SurnameTextBox" runat="server" Height="25px" Width="200px"></asp:TextBox></td>
           <asp:RequiredFieldValidator ID="SurnameRequired" runat="server" 
            ControlToValidate="SurnameTextBox" ErrorMessage="Surname is required." 
            ToolTip="Surname is required." ValidationGroup="RegistrationWizard">*</asp:RequiredFieldValidator> </tr>

      <tr> 
      <td align="right"> <asp:Label ID="DOBLabel" runat="server" AssociatedControlID="DOBTextBox">Date of Birth:</asp:Label> </td>
      <td> <asp:TextBox ID="DOBTextBox" runat="server" Height="25px" Width="200px">dd/mm/yyyy</asp:TextBox>
          </td>
           <asp:RequiredFieldValidator ID="DOBRequired" runat="server" 
            ControlToValidate="DOBTextBox" ErrorMessage="Date of birth is required." 
            ToolTip="Date of birth is required." ValidationGroup="RegistrationWizard">*</asp:RequiredFieldValidator> 
           <asp:CustomValidator runat="server"
            ID="DateRangeCustomValidator" 
            ControlToValidate="DOBTextBox"
            onservervalidate="valDateRange_ServerValidate" 
            ErrorMessage="Enter a valid date. " />
            </tr>

      <tr> 
      <td align="right"> <asp:Label ID="GenderLabel" runat="server" AssociatedControlID="GenderDropDownList">Gender:</asp:Label> </td>
      <td> <asp:DropDownList ID="GenderDropDownList" runat="server" AppendDataBoundItems="true" Height="27px" Width="205px">
                <asp:ListItem Value="-1">Select</asp:ListItem>
                <asp:ListItem>Male</asp:ListItem>
                <asp:ListItem>Female</asp:ListItem>
                </asp:DropDownList> </td>

           <asp:RequiredFieldValidator ID="GenderRequired" runat="server" 
            ControlToValidate="GenderDropDownList" ErrorMessage="Gender is required." 
            ToolTip="Gender is required." ValidationGroup="RegistrationWizard">*</asp:RequiredFieldValidator> 
            </tr>

      <tr> 
      <td align="right"> <asp:Label ID="PhoneNumberLabel" runat="server" AssociatedControlID="PhoneNumberTextBox">PhoneNumber:</asp:Label> </td>
      <td> <asp:TextBox ID="PhoneNumberTextBox" runat="server" Height="25px" Width="200px"></asp:TextBox></td></tr>

      <tr> 
      <td align="right"> <asp:Label ID="FavouriteTeamLabel" runat="server" AssociatedControlID="FavouriteTeamDropDownList">Favourite Team:</asp:Label> </td>
      <td> 
          <asp:DropDownList ID="FavouriteTeamDropDownList" runat="server" DataSourceID="Team" 
              DataTextField="Team" DataValueField="Team" Height="27px" Width="205px">
          </asp:DropDownList>
          <asp:SqlDataSource ID="Team" runat="server" 
              ConnectionString="<%$ ConnectionStrings:RocoSportsDBConnectionString %>" 
              SelectCommand="SELECT [Team] FROM [Team]"></asp:SqlDataSource>
          </td>
      </tr>

      <tr> 
      <td align="right"> <asp:Label ID="MaritialStatusLabel" runat="server" AssociatedControlID="MaritialStatusDropDownList">Maritial Status:</asp:Label> </td>
      <td> <asp:DropDownList ID="MaritialStatusDropDownList" runat="server" AppendDataBoundItems="true" Height="27px" Width="205px">
                <asp:ListItem Value="-1">Select</asp:ListItem>
                <asp:ListItem>Single</asp:ListItem>
                <asp:ListItem>Married</asp:ListItem>
                <asp:ListItem>Separated</asp:ListItem>
                <asp:ListItem>Unmarried Relationship</asp:ListItem>
                <asp:ListItem>Domestic Partnership</asp:ListItem>
                </asp:DropDownList> </td>
            </tr>

      <tr> 
      <td align="right"> <asp:Label ID="EducationLabel" runat="server" AssociatedControlID="EducationDropDownList">Education:</asp:Label> </td>
      <td> <asp:DropDownList ID="EducationDropDownList" runat="server" AppendDataBoundItems="true" Height="27px" Width="205px">
                <asp:ListItem Value="-1">Select</asp:ListItem>
                <asp:ListItem>None</asp:ListItem>
                <asp:ListItem>Primary School</asp:ListItem>
                <asp:ListItem>Secondary School</asp:ListItem>
                <asp:ListItem>College</asp:ListItem>
                <asp:ListItem>Undergrad</asp:ListItem>
                <asp:ListItem>Postgrad</asp:ListItem>
                </asp:DropDownList> </td>
            </tr>

      <tr> 
      <td align="right"> <asp:Label ID="ProfessionLabel" runat="server" AssociatedControlID="ProfessionDropDownList">Profession:</asp:Label> </td>
      <td> <asp:DropDownList ID="ProfessionDropDownList" runat="server" AppendDataBoundItems="true" Height="27px" Width="205px">
                <asp:ListItem Value="-1">Select</asp:ListItem>
                <asp:ListItem>None</asp:ListItem>
                <asp:ListItem>Management</asp:ListItem>
                <asp:ListItem>Professional</asp:ListItem>
                <asp:ListItem>Armed Forces</asp:ListItem>
                <asp:ListItem>Sales</asp:ListItem>
                <asp:ListItem>Adminstrative</asp:ListItem>
                <asp:ListItem>Farming</asp:ListItem>
                <asp:ListItem>Construction</asp:ListItem>
                </asp:DropDownList> </td>
            </tr>

      <tr> 
      <td align="right"> <asp:Label ID="SalaryLabel" runat="server" AssociatedControlID="SalaryDropDownList">Salary:</asp:Label> </td>
      <td> <asp:DropDownList ID="SalaryDropDownList" runat="server" AppendDataBoundItems="true" Height="27px" Width="205px">
                <asp:ListItem Value="-1">Select</asp:ListItem>
                <asp:ListItem>None</asp:ListItem>
                <asp:ListItem>5000</asp:ListItem>
                <asp:ListItem>10000</asp:ListItem>
                <asp:ListItem>20000</asp:ListItem>
                <asp:ListItem>30000</asp:ListItem>
                <asp:ListItem>50000</asp:ListItem>
                <asp:ListItem>100000</asp:ListItem>
                </asp:DropDownList> </td>
            </tr>

      <tr> 
      <td align="right"> <asp:Label ID="EmploymentStatusLabel" runat="server" AssociatedControlID="EmploymentStatusDropDownList">EmploymentStatus:</asp:Label> </td>
      <td> <asp:DropDownList ID="EmploymentStatusDropDownList" runat="server" AppendDataBoundItems="true" Height="27px" Width="205px">
                <asp:ListItem Value="-1">Select</asp:ListItem>
                <asp:ListItem>Unemployed</asp:ListItem>
                <asp:ListItem>Employed</asp:ListItem>
                <asp:ListItem>Self-employed</asp:ListItem>
                <asp:ListItem>Volunteer</asp:ListItem>
                </asp:DropDownList> </td>
            </tr>
    <tr> 
      <td align="right"> <asp:Label ID="InterestsLabel" runat="server" AssociatedControlID="InterestsCheckBox">Interests:</asp:Label> </td>
      <td>
          <asp:CheckBoxList ID="InterestsCheckBox" runat="server" 
              onselectedindexchanged="InterestsCheckBox_SelectedIndexChanged">
              <asp:ListItem>Reading</asp:ListItem>
              <asp:ListItem>Football</asp:ListItem>
              <asp:ListItem>Swimming</asp:ListItem>
              <asp:ListItem>Basketball</asp:ListItem>
              <asp:ListItem>Cooking</asp:ListItem>
              <asp:ListItem>Music</asp:ListItem>
              <asp:ListItem>Philosophy</asp:ListItem>
              <asp:ListItem>Programming</asp:ListItem>
          </asp:CheckBoxList>
        </td>
    </tr>


      <tr> <td align="center" colspan="2" style="color:Red;"> <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>  </td> </tr>
      <tr> <td align="center" colspan="2"> <asp:Button ID="RegisterButton" runat="server" Text="Register" onclick="registerbutton_Click"/> </td> </tr>
      </table>

导致此代码不起作用的问题可能是什么,但是没有抛出任何错误?另外,我想更好地定位验证器,并为它们添加颜色?

2 个答案:

答案 0 :(得分:3)

您已在验证器上设置ValidationGroup,但未在按钮上设置。

答案 1 :(得分:3)

除了DOB自定义验证程序之外,您正在为所有验证程序使用ValidationGroup。

将其添加到自定义验证器以及注册按钮。

<asp:CustomValidator runat="server"
                ID="DateRangeCustomValidator" 
                ControlToValidate="DOBTextBox"
                onservervalidate="valDateRange_ServerValidate" 
                ErrorMessage="Enter a valid date. " 
        ValidationGroup="RegistrationWizard"/>


<asp:Button ID="RegisterButton" runat="server" Text="Register"
 ValidationGroup="RegistrationWizard" onclick="registerbutton_Click"/>