Validate control in code behind in C# : By Your Function « Validation by Control « ASP.Net

ASP.Net
1. ADO.net Database
2. Ajax
3. Asp Control
4. Collections
5. Components
6. Data Binding
7. Development
8. File Directory
9. HTML Control
10. Language Basics
11. Login Security
12. Mobile Control
13. Network
14. Page
15. Request
16. Response
17. Server
18. Session Cookie
19. Sitemap
20. Theme Style
21. User Control and Master Page
22. Validation by Control
23. Validation by Function
24. WebPart
25. WPF
26. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
ASP.Net » Validation by Control » By Your Function 
Validate control in code behind in C#

<%@ Page language="c#" src="CustomerForm.aspx.cs" AutoEventWireup="false" Inherits="CustomerForm" %>
<HTML>
  <body>
    <form id="Form1" method="post" runat="server">
      <DIV style="BORDER-RIGHT: thin groove; BORDER-TOP: thin groove; FONT-SIZE: x-small; Z-INDEX: 101; LEFT: 16px; BORDER-LEFT: thin groove; WIDTH: 613px; BORDER-BOTTOM: thin groove; FONT-FAMILY: Verdana; POSITION: absolute; TOP: 20px; HEIGHT: 364px; BACKGROUND-COLOR: lightyellow"
        ms_positioning="GridLayout">
        <asp:TextBox id="txtUserName" style="Z-INDEX: 101; LEFT: 160px; POSITION: absolute; TOP: 16px"
          runat="server" Width="152px"></asp:TextBox>
        <asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 72px; POSITION: absolute; TOP: 19px" runat="server"
          Font-Bold="True">User Name:</asp:Label>
        <asp:TextBox id="txtPassword" style="Z-INDEX: 103; LEFT: 160px; POSITION: absolute; TOP: 56px"
          runat="server" TextMode="Password" Width="152px"></asp:TextBox>
        <asp:Label id="Label2" style="Z-INDEX: 104; LEFT: 80px; POSITION: absolute; TOP: 59px" runat="server"
          Font-Bold="True">Password:</asp:Label>
        <asp:TextBox id="txtRetype" style="Z-INDEX: 105; LEFT: 160px; POSITION: absolute; TOP: 88px"
          runat="server" TextMode="Password" Width="152px"></asp:TextBox>
        <asp:Label id="Label3" style="Z-INDEX: 106; LEFT: 13px; POSITION: absolute; TOP: 91px" runat="server"
          Font-Bold="True">Password (retype):</asp:Label>
        <asp:Label id="Label4" style="Z-INDEX: 107; LEFT: 104px; POSITION: absolute; TOP: 140px" runat="server"
          Font-Bold="True">E-mail:</asp:Label>
        <asp:TextBox id="txtEmail" style="Z-INDEX: 108; LEFT: 160px; POSITION: absolute; TOP: 136px"
          runat="server" Width="152px"></asp:TextBox>
        <asp:TextBox id="txtAge" style="Z-INDEX: 109; LEFT: 160px; POSITION: absolute; TOP: 176px" runat="server"
          Width="152px"></asp:TextBox>
        <asp:Label id="Label5" style="Z-INDEX: 110; LEFT: 117px; POSITION: absolute; TOP: 179px" runat="server"
          Width="32px" Height="16px" Font-Bold="True">Age:</asp:Label>
        <asp:Label id="Label6" style="Z-INDEX: 111; LEFT: 48px; POSITION: absolute; TOP: 228px" runat="server"
          Width="106px" Height="24px" Font-Bold="True">Referrer Code:</asp:Label>
        <asp:TextBox id="txtCode" style="Z-INDEX: 112; LEFT: 160px; POSITION: absolute; TOP: 224px" runat="server"
          Width="152px"></asp:TextBox>
        <asp:RequiredFieldValidator id="vldUserName" style="Z-INDEX: 113; LEFT: 336px; POSITION: absolute; TOP: 18px"
          runat="server" ErrorMessage="You must enter a user name." ControlToValidate="txtUserName"></asp:RequiredFieldValidator>
        <asp:RequiredFieldValidator id="vldPassword" style="Z-INDEX: 115; LEFT: 336px; POSITION: absolute; TOP: 59px"
          runat="server" ErrorMessage="You must enter a password." ControlToValidate="txtPassword"></asp:RequiredFieldValidator>
        <asp:CompareValidator id="vldRetype" style="Z-INDEX: 114; LEFT: 336px; POSITION: absolute; TOP: 93px"
          runat="server" ErrorMessage="Your password does not match." ControlToCompare="txtPassword" ControlToValidate="txtRetype"></asp:CompareValidator>
        <asp:RegularExpressionValidator id="vldEmail" style="Z-INDEX: 117; LEFT: 337px; POSITION: absolute; TOP: 139px"
          runat="server" ErrorMessage="This email is missing the @ symbol." ValidationExpression=".+@.+" ControlToValidate="txtEmail"></asp:RegularExpressionValidator>
        <asp:RangeValidator id="vldAge" style="Z-INDEX: 116; LEFT: 337px; POSITION: absolute; TOP: 180px" runat="server"
          ErrorMessage="This age is not between 0 and 120." Type="Integer" MaximumValue="120" MinimumValue="0"
          ControlToValidate="txtAge"></asp:RangeValidator>
        <asp:CustomValidator id="vldCode" style="Z-INDEX: 118; LEFT: 337px; POSITION: absolute; TOP: 227px" runat="server"
          ErrorMessage="Try a string that starts with 014." ControlToValidate="txtCode" ClientValidationFunction="MyCustomValidation"></asp:CustomValidator>
        <asp:Button id="cmdSubmit" style="Z-INDEX: 119; LEFT: 160px; POSITION: absolute; TOP: 304px"
          runat="server" Width="120px" Text="Submit"></asp:Button>
        <asp:Button id="cmdCancel" style="Z-INDEX: 120; LEFT: 304px; POSITION: absolute; TOP: 304px"
          runat="server" Width="121px" Height="24px" Text="Cancel" CausesValidation="False"></asp:Button></DIV>
      <asp:Label id="lblMessage" style="Z-INDEX: 102; LEFT: 24px; POSITION: absolute; TOP: 388px"
        runat="server" Width="616px" Height="72px"></asp:Label>
    </form>
  </body>
</HTML>


<%-- CustomerForm.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
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 CustomerForm : System.Web.UI.Page
  {
    protected System.Web.UI.WebControls.TextBox txtUserName;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.TextBox txtPassword;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.TextBox txtRetype;
    protected System.Web.UI.WebControls.Label Label3;
    protected System.Web.UI.WebControls.Label Label4;
    protected System.Web.UI.WebControls.TextBox txtEmail;
    protected System.Web.UI.WebControls.TextBox txtAge;
    protected System.Web.UI.WebControls.Label Label5;
    protected System.Web.UI.WebControls.Label Label6;
    protected System.Web.UI.WebControls.TextBox txtCode;
    protected System.Web.UI.WebControls.RequiredFieldValidator vldUserName;
    protected System.Web.UI.WebControls.RequiredFieldValidator vldPassword;
    protected System.Web.UI.WebControls.CompareValidator vldRetype;
    protected System.Web.UI.WebControls.RegularExpressionValidator vldEmail;
    protected System.Web.UI.WebControls.RangeValidator vldAge;
    protected System.Web.UI.WebControls.CustomValidator vldCode;
    protected System.Web.UI.WebControls.Button cmdSubmit;
    protected System.Web.UI.WebControls.Button cmdCancel;
    protected System.Web.UI.WebControls.Label lblMessage;
  
    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.vldCode.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.vldCode_ServerValidate);
      this.cmdSubmit.Click += new System.EventHandler(this.cmdSubmit_Click);
      this.cmdCancel.Click += new System.EventHandler(this.cmdCancel_Click);
      this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void cmdSubmit_Click(object sender, System.EventArgs e)
    {
      if (!this.IsValidreturn;
      lblMessage.Text = "This is a valid form.";
    }

    private void cmdCancel_Click(object sender, System.EventArgs e)
    {
        lblMessage.Text = "No attempt was made to validate this form.";
    }

    private void vldCode_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs e)
    {
      try{
        int val = Int32.Parse(e.Value.Substring(03));
        if (val % == 0){
          e.IsValid = true;
        }else{
          e.IsValid = false;
        }
      }catch{
        e.IsValid = false;
      }
    }
  }


--%>
           
       
Related examples in the same category
1. Implement custom validation control event (VB.net)
2. Validate control manually in C#
3. Date validation: start, end date (C#)
4. Validate by a Server Validate function (VB.net)
5. Validating a Percentage Using the CustomValidator Control (VB.net)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.