ASP.Net/Validation by Control/Javascript Validation

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Display ValidationSummary in a client side dialog (C#)

   <source lang="csharp">

<%@ Page Language="C#" %> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <title>Show Summary Popup</title>

</head> <body>

   <form id="form1" runat="server">
   <asp:ValidationSummary
       id="ValidationSummary1"
       ShowMessageBox="true"
       ShowSummary="false"
       Runat="server" />
   
   <asp:Label
       id="lblFirstName"
       Text="First Name:"
       AssociatedControlID="txtFirstName"
       Runat="server" />
   
<asp:TextBox id="txtFirstName" Runat="server" /> <asp:RequiredFieldValidator id="reqFirstName" ErrorMessage="First Name is required" ControlToValidate="txtFirstName" Display="None" Runat="server" />

<asp:Label id="lblLastName" Text="Last Name:" AssociatedControlID="txtLastName" Runat="server" />
<asp:TextBox id="txtLastName" Runat="server" /> <asp:RequiredFieldValidator id="reqLastName" ErrorMessage="Last Name is required" ControlToValidate="txtLastName" Display="None" Runat="server" />

<asp:Button id="btnSubmit" Text="Submit" Runat="server" />
   </form>

</body> </html>

      </source>
   
  


Use Javascript and asp validation control together (VB.net)

   <source lang="csharp">

<%@ Page Language="vb" %> <html> <head>

  <title>Validation Control Example</title>
   <script language="javascript">
   
  </script>
  <script runat="server">
     Sub Page_Load()
        vsSummary.DisplayMode = ValidationSummaryDisplayMode.List
     End Sub
     Sub ServerValidation (source As object, args As ServerValidateEventArgs)
        Dim RegExVal As New System.Text.RegularExpressions.Regex("^\d{4}$")
        If RegExVal.IsMatch(args.Value) Then
           args.IsValid = True
        Else
           args.IsValid = False
        End If
     End Sub
  </script>

</head> <body>

Validation Control Example

  <form runat="server">
     <asp:table id="MyTable" border="1" cellpadding="5" cellspacing="0" runat="server">
        <asp:tablerow runat="server">
           <asp:tablecell runat="server">
              Compare Validator Control:
              

Enter two numbers to compare </asp:tablecell> <asp:tablecell runat="server"> <asp:textbox id="value1" runat="server"/>
<asp:textbox id="value2" runat="server"/>
<asp:comparevalidator id="cvCompare" controltovalidate="value1" controltocompare="value2" operator="equal" type="integer" errormessage="Fields are not equal!" display="dynamic" runat="server"/> </asp:tablecell> </asp:tablerow>
        <asp:tablerow runat="server">
           <asp:tablecell runat="server">
              ValidationSummary Control:
           </asp:tablecell>
           <asp:tablecell runat="server">
              <asp:validationsummary id="vsSummary"
                 displaymode="bulletlist" 
                 headertext="Page has the following errors: "
                 showsummary="true" 
                 showmessagebox="false"
                 runat="server"/>
           </asp:tablecell>
        </asp:tablerow>
        <asp:tablerow runat="server">
           <asp:tablecell colspan="2" runat="server">
              <asp:button text="submit" runat="server"/>
           </asp:tablecell>
        </asp:tablerow>
     </asp:table>
     <asp:label id="MyLabel" runat="server"/>
  </form>

</body> </html>

      </source>
   
  


Validation summary in javascript dialog (C#)

   <source lang="csharp">

<%@ Page language="c#" src="ValidationSummary.aspx.cs" AutoEventWireup="false" Inherits="MyOwnValidationSummary" %> <HTML>

 <body>
   <form id="Form1" method="post" runat="server">

A number (1 to 10): <asp:textbox id="txtValidated" runat="server"></asp:textbox>  <asp:rangevalidator id="RangeValidator" runat="server" Display="None" Type="Integer" MinimumValue="1" MaximumValue="10" ControlToValidate="txtValidated" ErrorMessage="The First Number Is Not In The Range"></asp:rangevalidator>

        Not validated:  <asp:textbox id="txtNotValidated" runat="server"></asp:textbox>


<asp:button id="cmdOK" runat="server" Text="OK"></asp:button>

<asp:label id="lblMessage" runat="server" EnableViewState="False"></asp:label>

<asp:ValidationSummary id="ValidationSummary1" runat="server" ShowMessageBox="True"></asp:ValidationSummary>

   </form>
 </body>

</HTML>

<%-- using System; using System.Collections; using System.ruponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;

 public class MyOwnValidationSummary : System.Web.UI.Page
 {
   protected System.Web.UI.WebControls.TextBox txtValidated;
   protected System.Web.UI.WebControls.RangeValidator RangeValidator;
   protected System.Web.UI.WebControls.TextBox txtNotValidated;
   protected System.Web.UI.WebControls.Button cmdOK;
   protected System.Web.UI.WebControls.Label lblMessage;
   protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1;
 
   private void Page_Load(object sender, System.EventArgs e)
   {
     // Put user code to initialize the page here
   }
   #region Web Form Designer generated code
   override protected void OnInit(EventArgs e)
   {
     //
     // CODEGEN: This call is required by the ASP.NET Web Form Designer.
     //
     InitializeComponent();
     base.OnInit(e);
   }
   
   /// <summary>
   /// Required method for Designer support - do not modify
   /// the contents of this method with the code editor.
   /// </summary>
   private void InitializeComponent()
   {    
     this.cmdOK.Click += new System.EventHandler(this.cmdOK_Click);
     this.Load += new System.EventHandler(this.Page_Load);
   }
   #endregion
   private void cmdOK_Click(object sender, System.EventArgs e)
   {
     if (!this.IsValid) return;
     lblMessage.Text = "cmdOK_Click event handler executed.";
   }
 }

--%>

      </source>