ServerValidateEventArgs.IsValid プロパティ

定義

Value プロパティによって指定された値の検証が成功したかどうかを取得または設定します。

public:
 property bool IsValid { bool get(); void set(bool value); };
public bool IsValid { get; set; }
member this.IsValid : bool with get, set
Public Property IsValid As Boolean

プロパティ値

Value プロパティによって指定された値の検証が成功した場合は true。それ以外の場合は false

次の例では、 プロパティを使用 IsValid して検証の結果を格納する方法を示します。


<%@ Page Language="C#" AutoEventWireup="True" %>

<!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>
    <title>CustomValidator ServerValidate Example</title>
<script runat="server">

      void ValidateBtn_OnClick(object sender, EventArgs e) 
      { 

         // Display whether the page passed validation.
         if (Page.IsValid) 
         {

            Message.Text = "Page is valid.";

         }

         else 
         {

            Message.Text = "Page is not valid!";

         }

      }

      void ServerValidation(object source, ServerValidateEventArgs args)
      {

         try 
         {

            // Test whether the value entered into the text box is even.
            int i = int.Parse(args.Value);
            args.IsValid = ((i%2) == 0);

         }

         catch(Exception ex)
         {

            args.IsValid = false;

         }

      }

   </script>    

</head>
<body>

   <form id="form1" runat="server">
  
      <h3>CustomValidator ServerValidate Example</h3>

      <asp:Label id="Message"  
           Text="Enter an even number:" 
           Font-Names="Verdana" 
           Font-Size="10pt" 
           runat="server"
           AssociatedControlID="Text1"/>

      <br />

      <asp:TextBox id="Text1" 
           runat="server" />
    
        

      <asp:CustomValidator id="CustomValidator1"
           ControlToValidate="Text1"
           Display="Static"
           ErrorMessage="Not an even number!"
           ForeColor="green"
           Font-Names="verdana" 
           Font-Size="10pt"
           OnServerValidate="ServerValidation"
           runat="server"/>

      <br />
 
      <asp:Button id="Button1"
           Text="Validate" 
           OnClick="ValidateBtn_OnClick" 
           runat="server"/>

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

<%@ Page Language="VB" AutoEventWireup="True" %>

<!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>
    <title>CustomValidator ServerValidate Example</title>
<script runat="server">

      Sub ValidateBtn_OnClick(sender As Object, e As EventArgs) 

         ' Display whether the page passed validation.
         If Page.IsValid Then 

            Message.Text = "Page is valid."

         Else 

            Message.Text = "Page is not valid!"

         End If

      End Sub

      Sub ServerValidation(source As Object, args As ServerValidateEventArgs)

         Try 

            ' Test whether the value entered into the text box is even.
            Dim num As Integer = Integer.Parse(args.Value)
            args.IsValid = ((num mod 2) = 0)
 
         Catch ex As Exception
         
            args.IsValid = false

         End Try

      End Sub

   </script>    

</head>
<body>

   <form id="form1" runat="server">
  
      <h3>CustomValidator ServerValidate Example</h3>

      <asp:Label id="Message"
           Text="Enter an even number:" 
           Font-Names="Verdana" 
           Font-Size="10pt" 
           runat="server"
           AssociatedControlID="Text1" />

      <br />

      <asp:TextBox id="Text1" 
           runat="server" />
    
        

      <asp:CustomValidator id="CustomValidator1"
           ControlToValidate="Text1"
           Display="Static"
           ErrorMessage="Not an even number!"
           ForeColor="green"
           Font-Names="verdana" 
           Font-Size="10pt"
           OnServerValidate="ServerValidation"
           runat="server"/>

      <br />
 
      <asp:Button id="Button1"
           Text="Validate" 
           OnClick="ValidateBtn_OnClick" 
           runat="server"/>

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

注釈

検証ルーチンが完了したら、 プロパティを IsValid 使用して、プロパティで指定された値が検証に Value 合格したかどうかを示します。 この値は、コントロールに関連付けられている入力コントロールが検証に CustomValidator 合格したかどうかを決定します。

適用対象

こちらもご覧ください