Articles → .NET → Creating Your Own Validation Control In Asp.Net

Creating Your Own Validation Control In Asp.Net










Prerequisite




  1. hands on experience in creating an ASP.NET web application.
  2. Basic knowledge of validation controls in ASP.NET.

Steps For The Creation Of Validation Control




  1. Create a new class and inherit the class from BaseValidator class.
  2. Override the method EvaluateIsValid and write validation logic inside it.
  3. Write properties if required.



Scenario






public class MaxItemsValidator: BaseValidator {
  public MaxItemsValidator() {

}
}




public class MaxItemsValidator: BaseValidator {
  public MaxItemsValidator() {

}
  protected override bool EvaluateIsValid() {}
}






public int MaximumSize {
  get;
  set;
}




protected override bool EvaluateIsValid() {
  ListBox lb = (ListBox) this.FindControl(ControlToValidate);
  bool status = true;
  if (lb.Items.Count > MaximumSize) {
    status = false;
  }
  return status;
}
















<%@ Register Namespace="MyValidators" TagPrefix="val" %>




<asp:ListBox runat="server" ID="lstLeftSide" 
SelectionMode="Multiple">
	<asp:ListItem>Skill 1</asp:ListItem>
	<asp:ListItem>Skill 2</asp:ListItem>
	<asp:ListItem>Skill 3</asp:ListItem>
	<asp:ListItem>Skill 4</asp:ListItem>
	<asp:ListItem>Skill 5</asp:ListItem>
	<asp:ListItem>Skill 6</asp:ListItem>
	<asp:ListItem>Skill 7</asp:ListItem>
	<asp:ListItem>Skill 8</asp:ListItem>
</asp:ListBox>
<asp:Button ID="btnTransferToRight" runat="server" Text=">>" 
onclick="btnTransferToRight_Click" />
<asp:Button ID="btnTransferToLeft" runat="server" Text="<<" 
onclick="btnTransferToLeft_Click" />
	<asp:ListBox runat="server" ID="lstRightSide" 
	SelectionMode="Multiple"></asp:ListBox>
	<asp:Button runat="server" Text="Trigger" ID="btnTrigger" 
onclick=" btnTrigger _Click" />
	<val:MaxItemsValidator runat="server" ControlToValidate="lstRightSide" ErrorMessage="Max limit exceeded" MaximumSize="5"></val:MaxItemsValidator>  
















Picture showing the output of creating your first validation control in asp.net
Click to Enlarge




Validation Control Code


using System.Web.UI.WebControls;

namespace MyValidators {
  /// <summary>
  /// Summary description for MaxItemsValidator
  /// </summary>
  public class MaxItemsValidator: BaseValidator {
    public MaxItemsValidator() {
      //
      // TODO: Add constructor logic here
      //
    }

    protected override bool EvaluateIsValid() {
      ListBox lb = (ListBox) this.FindControl(ControlToValidate);
      bool status = true;
      if (lb.Items.Count > MaximumSize) {
        status = false;
      }
      return status;
    }

    public int MaximumSize {
      get;
      set;
    }
  }
}



Web Form ASPX Code


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="NewValidatorConsumer.aspx.cs" Inherits="NewValidatorConsumer" %><%@ Register Namespace="MyValidators" TagPrefix="val" %>
<!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></title>
	</head>
	<body>
		<form id="form1" runat="server">
			<div>
				<asp:ListBox runat="server" ID="lstLeftSide" SelectionMode="Multiple">
					<asp:ListItem>Skill 1</asp:ListItem>
					<asp:ListItem>Skill 2</asp:ListItem>
					<asp:ListItem>Skill 3</asp:ListItem>
					<asp:ListItem>Skill 4</asp:ListItem>
					<asp:ListItem>Skill 5</asp:ListItem>
					<asp:ListItem>Skill 6</asp:ListItem>
					<asp:ListItem>Skill 7</asp:ListItem>
					<asp:ListItem>Skill 8</asp:ListItem>
				</asp:ListBox>
				<asp:Button ID="btnTransferToRight" runat="server" Text=" >>" 
            onclick="btnTransferToRight_Click" />
				<asp:Button ID="btnTransferToLeft" runat="server" Text=" <
					<" 
            onclick="btnTransferToLeft_Click" />
					<asp:ListBox runat="server" ID="lstRightSide" SelectionMode="Multiple"></asp:ListBox>
					<asp:Button runat="server" Text="Trigger" ID="btnTrigger" onclick="btnTrigger_Click" />
					<val:MaxItemsValidator runat="server" ControlToValidate="lstRightSide" ErrorMessage="Max limit exceeded" MaximumSize="5"></val:MaxItemsValidator>
				</div>
			</form>
		</body>
	</html>



Code Behind


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class NewValidatorConsumer: System.Web.UI.Page {

  protected void btnTrigger_Click(object sender, EventArgs e) {

}
  protected void btnTransferToRight_Click(object sender, EventArgs e) {
    if (lstLeftSide.SelectedIndex > -1) {
      lstRightSide.Items.Add(lstLeftSide.Items[lstLeftSide.SelectedIndex]);
      lstLeftSide.Items.Remove(lstLeftSide.Items[lstLeftSide.SelectedIndex]);
    }
  }
  protected void btnTransferToLeft_Click(object sender, EventArgs e) {
    if (lstRightSide.SelectedIndex > -1) {
      lstLeftSide.Items.Add(lstRightSide.Items[lstRightSide.SelectedIndex]);
      lstRightSide.Items.Remove(lstRightSide.Items[lstRightSide.SelectedIndex]);
    }
  }
}



Posted By  -  Karan Gupta
 
Posted On  -  Friday, October 14, 2011

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250