<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<script language="javascript">
function ClientValidator(source, args)
{
if (args.Value % 2 == 0)
args.IsValid=true;
else
args.IsValid=false;
return;
}
</script>
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:TextBox ID="txtValue" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Submit" />
<asp:CustomValidator
ControlToValidate="txtValue"
ValidateEmptyText=false
ID="CustomValidator1" runat="server"
ClientValidationFunction="ClientValidator"
OnServerValidate="ServerValidator">
Please enter an even number
</asp:CustomValidator>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default_aspx : System.Web.UI.Page
{
protected void ServerValidator(object source, ServerValidateEventArgs e)
{
try
{
int evenNumber = Int32.Parse(e.Value);
if (evenNumber % 2 == 0)
e.IsValid = true;
}
catch (Exception)
{
// error handler here
}
}
}
|