Articles → .NET → Creating your own validation control in asp.net

Creating your own validation control in asp.net




Introduction









Prerequisite









Steps for the creation of validation control













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>  

















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