Sunday, January 12, 2014

Server Side Validation - CustomValidator

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="DropDownlist_Default3" %>
<!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>
    Custom text:<br />
<asp:TextBox runat="server" id="txtCustom" />
<asp:CustomValidator runat="server" id="cusCustom" controltovalidate="txtCustom" onservervalidate="cusCustom_ServerValidate" errormessage="The text must be exactly 8 characters long!" />
<br /><br />
<asp:Button ID="btn1" runat="server" Text="Submit" OnClick="btn1_Click" />
    </div>
    </form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class DropDownlist_Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       // cusCustom.Validate();
    }
    protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e)
    {
        if (e.Value.Length == 8)
            e.IsValid = true;
        else
            e.IsValid = false;
    }
    protected void btn1_Click(object sender, EventArgs e)
    {
        if (cusCustom.IsValid)
        {
        }
        else
        {
        }
    }
}

0 comments:

Post a Comment