What is an Validation Groups in 2.0 and how can u create them?

Platform: ASP.NET| Category: Custom Controls

Validation Groups allows the different section of the webpage to be validated seperately. All the ASP.NET 2.0 Validation controls,interactive form controls like TextBox,list controls Button and other controls that are used to submit a webform have validationGroup property. The default value of this property is the empty string. When a button with a ValidationGroup property is clicked all of the validation controls on the page with the same ValidationGroup property value are executed.

On postback, the Page.IsValid property checks the validity of all of the validation controls. Programmatically it can be checked by the Validate() Method.


<html>
	<head runat='server'>
		<title>Validation Groups</title>
		<script runat='server'>
			void Group1Click(Object s, EventArgs e)
			{
				if (Page.IsValid)
				{
                         		                	lblResult.Text = 'Name Validated & Submitted';
				}
			}

			void Group2Click(Object s, EventArgs e)
			{
				if (Page.IsValid)
				{
					lblResult.Text = 'Age Validated & Submitted';
				}
			}
		</script>

	</head>
	<body>
		<form id='form1' runat='server'>
			<div>
				<asp:Label ID='lblResult' Runat='Server' />
    				<fieldset style='padding:20px'>
					<legend>Name</legend>
					<asp:Label ID='Name' runat='server' Text='Name:'></asp:Label>
	       				<asp:TextBox id='TextBox1' Runat='Server'/>
					<asp:Button ID='Button1' ValidationGroup='Group1' Text='Submit' 
						OnClick='Group1Click'  Runat='Server' />
					<asp:RequiredFieldValidator ID='RequiredFieldValidator1' 
						ValidationGroup='Group1'  ControlToValidate='TextBox1'
					       	Text='Field Cannot Be Empty' Runat='Server' />
				</fieldset>
        
    				<fieldset style='padding:20px'>
					<legend>Age</legend>
					<asp:Label ID='Label1' runat='server' Text='Age:'></asp:Label>
        
    					<asp:TextBox  id='TextBox2' Runat='Server' />
					<asp:Button ID='Button2'   ValidationGroup='Group2' Text='Submit'
						  OnClick='Group2Click'  Runat='Server' />
					<asp:RangeValidator ID='RangeValidator1' runat='server' ErrorMessage='Enter Age 
						   between 18 To 58'  MaximumValue='58' MinimumValue='18' 
						   ValidationGroup='Group2' ControlToValidate='TextBox2'>
					            Enter Age between 18 To 58</asp:RangeValidator>
				</fieldset>
        			</div>
    		</form>
	</body>
</html>

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.