March 5, 2011

Custom Validator for Checkbox server control

Custom Validator for Checkbox

ExCustomValidator.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExCustomValidator.aspx.cs" Inherits="ExCusomValidator" %>


<html>
<head>
</head>
<body>
    <form id="Form1" runat="server">
        <p>
            Check checkbox For Accepting the terms n conditons 
        </p>
        <p>
            <asp:CheckBox id="CheckBox1" runat="server" 
             Text=" Accept Terms and conditions"></asp:CheckBox>
            &nbsp; 
            <asp:CustomValidator id="CustomValidator1" 
             runat="server" ErrorMessage="Please Accept Trems and Conditions" 
             OnServerValidate="CustomValidator1_ServerValidate">
            </asp:CustomValidator>
        </p>
        <p>
            <asp:Button id="Button1" onclick="Button1_Click" 
             runat="server" Text="Submit"></asp:Button>
        </p>
        <p>
            <asp:Label id="Label1" runat="server"></asp:Label>
        </p>
       
    </form>
</body>
</html>

ExCustomValidator.aspx.cs


using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 ExCusomValidator : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


    }


    public void CustomValidator1_ServerValidate(Object source,
     ServerValidateEventArgs args)
    {


        args.IsValid = (CheckBox1.Checked == true);
    }


    public void Button1_Click(Object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            Label1.Text = "Thank you for Accepting";
        }
        else
        {
            Label1.Text = "";
        }
    }
}

No comments:

Post a Comment