Textbox + Custom Validation + Validation Summary 使用方法

   

   code behind page
 
   Protected Sub CV1SV(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CV1.ServerValidate
 
      Dim ABC As Boolean
 
      If args.Value = "text1" Then
         ABC = True
      Else
         CV1.ErrorMessage = "Textbox1 is not "text1""
         ABC = False
      End If
 
      If Not (args.Value = "") Then
         args.IsValid = ABC And True
      Else
         CV1.ErrorMessage = "Textbox1 is not empty"
         args.IsValid = ABC And False
      End If
 
   End Sub
 
   Protected Sub CV2SV(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CV2.ServerValidate
 
      Dim ABC As Boolean
 
      If args.Value = "text2" Then
         ABC = True
      Else
         CV2.ErrorMessage = "Textbox2 is not "text2""
         ABC = False
      End If
 
      If Not (args.Value = "") Then
         args.IsValid = ABC And True
      Else
         CV2.ErrorMessage = "Textbox2 is not empty"
         args.IsValid = ABC And False
      End If
 
   End Sub
 
   Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
 
      If IsValid Then
         Response.Write("Completed!!")
      Else
         Response.Write("Failure!!")
      End If
 
   End Sub
 

   

   aspx frontpage
   

   <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
   
   <!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">
   <title>Untitled Page</title>
   </head>
   
   <body>
   
      <form id="form1" runat="server">
      <div>
 
         <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="A" />
 
         <asp:TextBox ID="TextBox1" runat="server" ValidationGroup="A" />
 
         <asp:CustomValidator ID="CV1" runat="server"
         ControlToValidate="TextBox1" ErrorMessage="CustomValidator" ValidateEmptyText="true"
         onservervalidate="CV1SV" ValidationGroup="A"/><br/>
 
         <asp:TextBox ID="TextBox2" runat="server" ValidationGroup="A" />
 
         <asp:CustomValidator ID="CV2" runat="server"
         ControlToValidate="TextBox2" ErrorMessage="CustomValidator" ValidateEmptyText="true"
         onservervalidate="CV2SV" ValidationGroup="A"/><br/>
 
         <asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="A"/>
 
      </div>
      </form>
   
   </body>
   </html>