Powered by Blogger.

Saturday, February 15, 2014

ValidationGroup example in Asp.Net

In this post we will discuss how to use ValidationGroup in Asp.Net.

You can also check my previous posts on  ValidationSummary, RegularExpressionValidator, RangeValidator, RequiredFieldValidator example in Asp.Net.

If your page has several groups of controls and you want to perform validation separatly then ValidationGroup will be very much helpful.

You have to assign ValidationGroup to controls, validators and buttons. Here in this example we have two pannels.

Below is the .aspx code:

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

<!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 id="Head1" runat="server">
    <title>ValidationGroup example in Asp.Net</title>
    <form id="form1" runat="server">
        <asp:Panel ID="pnlLogin" runat="server">
            Enter EmailID:<asp:TextBox ID="txtEmailIDLogin" runat="server" ValidationGroup="Group1"></asp:TextBox><br />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ForeColor="Red"
                ValidationGroup="Group1" ErrorMessage="Enter Email ID!" ControlToValidate="txtEmailIDLogin"></asp:RequiredFieldValidator><br />
            <asp:Button ID="btnLogin" runat="server" Text="Submit" ValidationGroup="Group1" />
        <asp:Panel ID="pnlSignup" runat="server">
            Enter EmailID:
            <asp:TextBox ID="txtEmailIDSignUp" runat="server" ValidationGroup="Group2"> </asp:TextBox>
            <br />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ForeColor="Red"
                ErrorMessage="Enter Email ID!" ControlToValidate="txtEmailIDSignUp" ValidationGroup="Group2"></asp:RequiredFieldValidator>
            <br />
            <asp:Button ID="btnSignup" runat="server" Text="Submit" ValidationGroup="Group2" />

Here if you click on first button then only 1st textbox will get validated. And also if you click on second button then 2nd textbox will get validated like below figure.