ASP.NET grouping is implemented by setting ValidationGroup property of RequiredFieldValidator to ensure that the user has entered the data into input control. The RequireField control belongs to System.Web.UI.WebControls
Syntax
<asp:RequiredFieldValidator id="reqUserName"
ControlToValidate="txtUserName" Text="(Required)"
ValidationGroup="LoginGroup" Runat="server" />
Property | Description |
---|---|
ControlToValidate | Specifies control name which needs to be validate. |
Display | Specifies behavior of the error message. |
EnableClientScript | Specifies whether or not client-side validation is enabled. |
Enabled | Specifies whether or not validation control is enabled or not. |
ErrorMessage | Specifies error messageto be display in validation summary. |
ForeColor | Specifies error message color. |
IsValid | Indicate if associate control passes validation or not. |
SetFocusOnError | Set focus on associate control if validation fails. |
Text | Specifies text to be display if validation fails. |
ValidationGroup | Specifies validation group name. |
Validate | Update the isvalid propery. |
ValidateGetValidationProperty | Determineing validation property of a control, if it exists. |
InitialValue | Specifies initial value of input control. |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ASP.NET RequiredFieldValidator Group Example</title>
</head>
<body>
<form id="Form" runat="server">
<div style="width: 619px">
<asp:Label ID="lblTitle" runat="server" Font-Bold="True" Font-Size="Medium"
Font-Underline="True" Text="ASP.NET RequiredFieldValidator Grouping Example"></asp:Label><br /><br />
<fieldset>
<table border="0" width="600px" cellpadding="2" cellspacing="0">
<tr><td>
<asp:Label id="lblUserName" Text="Employee G1 Name:"
AssociatedControlID="txtUserName" Runat="server" />
<asp:TextBox id="txtUserName" Runat="server" />
<asp:RequiredFieldValidator id="reqUserName"
ControlToValidate="txtUserName" Text="(Required)"
ValidationGroup="LoginGroup" Runat="server" />
</td></tr>
<tr><td>
<asp:Label id="lblPassword" Text="Employee G1 Password:"
AssociatedControlID="txtPassword" Runat="server" />
<asp:TextBox id="txtPassword" TextMode="Password"
Runat="server" />
<asp:RequiredFieldValidator id="reqPassword"
ControlToValidate="txtPassword" Text="(Required)"
ValidationGroup="LoginGroup" Runat="server" />
</td></tr>
<tr><td>
<asp:Button id="btnLogin" Text="G1 Login"
ValidationGroup="LoginGroup" Runat="server" />
</td></tr>
<tr><td>
<asp:Label id="lblFirstName"
Text="Employee G2 Name:" AssociatedControlID="txtFirstName"
Runat="server" />
<asp:TextBox id="txtFirstName" Runat="server" />
<asp:RequiredFieldValidator id="reqFirstName"
ControlToValidate="txtFirstName" Text="(Required)"
ValidationGroup="RegisterGroup" Runat="server" />
</td></tr>
<tr><td>
<asp:Label id="lblEmpG2" Text="Employee G2 Password::"
AssociatedControlID="txtPassword" Runat="server" />
<asp:TextBox id="txtPassG2" Runat="server" TextMode="Password"/>
<asp:RequiredFieldValidator id="reqLastName"
ControlToValidate="txtPassG2" Text="(Required)"
ValidationGroup="RegisterGroup" Runat="server" />
</td></tr>
<tr><td>
<asp:Button id="btnRegister"
Text="G2 Login" ValidationGroup="RegisterGroup"
Runat="server" />
</td></tr>
</table>
</fieldset>
</div>
</form>
</html>
Above example will produce following output