自定义验证器不在asp.net中工作

时间:2013-08-24 14:15:46

标签: asp.net validation

为什么不起作用?

如果用户在下拉列表中选择“Programmer”并输入“12”到文本框,我希望激活验证。但是当我点击提交按钮时没有发生任何事情。

   <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>

   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

   <html xmlns="http://www.w3.org/1999/xhtml">
   <head runat="server">
   <script language="JavaScript">
   var text = document.getElementById("Textbox2");
   function Date(oSrc, args) {
   args.IsValid = (args.Value == "Programmer" && text == "12");
   }
   </script>

   <title></title>
   </head>
   <body>
   <form id="form1" runat="server">
   <div>

   <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
   <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
   <asp:Button ID="Button1" runat="server" Text="Button" />
   <asp:CustomValidator ID="dateValidator" runat="server" 
   ClientValidationFunction="Date" ControlToValidate="DropDownList1" Display="Dynamic" 
   ErrorMessage="Sample error!"></asp:CustomValidator>

   <asp:DropDownList ID="DropDownList1" runat="server">
  <asp:ListItem Selected="True">Select a profession</asp:ListItem>
  <asp:ListItem>Programmer</asp:ListItem>
  <asp:ListItem>Lawyer</asp:ListItem>
  <asp:ListItem>Doctor</asp:ListItem>
  <asp:ListItem>Artist</asp:ListItem>
   </asp:DropDownList>

   </div>
   </form>
   </body>
   </html>

1 个答案:

答案 0 :(得分:2)

尝试以下

<script language="JavaScript">
   function Date(oSrc, args) {
   args.IsValid = (args.Value == "Programmer" && document.getElementById('<%=TextBox2.ClientID%>').value== "12");
}