Labels

Thursday, January 3, 2008

Validation Groups

Validation Groups are required in two cases.

· To separate the Validations for controls contained in two separate Divs, Panels.

· To separate the Validations for controls contianed in User Control and the Container Page.

In ASP.NET 2.0, you no longer face this problems in implementing above. The ASP.NET 2.0 Framework introduces the idea of Validation Groups. A validation group enables you to group related form fields together.

In first case, all is required to introduce the ‘ValidationGroup property for Validation Controls and the Button control only. [Not for any Textbox controls].

<form id="form1" runat="server">

<div style="border: 1px solid blue; float: left; width: 100px">

<fieldset>

<legend>Login</legend>

<asp:Label ID="lblUserName" Text="User Name:" AssociatedControlID="txtUserName" runat="server" />

<br />

<asp:TextBox ID="txtUserName" runat="server" />

<asp:RequiredFieldValidator ID="reqUserName" ControlToValidate="txtUserName" Text="(Required)"

ValidationGroup="LoginGroup" runat="server" />

<asp:Button ID="btnLogin" Text="Login" ValidationGroup="LoginGroup" runat="server" />

</fieldset>

</div>

<div style="border: 1px solid blue; float: left; width: 100px">

<fieldset>

<legend>Register</legend>

<asp:Label ID="lblFirstName" Text="First Name:" AssociatedControlID="txtFirstName"

runat="server" />

<br />

<asp:TextBox ID="txtFirstName" runat="server" />

<asp:RequiredFieldValidator ID="reqFirstName" ControlToValidate="txtFirstName" Text="(Required)"

ValidationGroup="RegisterGroup" runat="server" />&nbsp;<br />

<asp:Button ID="btnRegister" Text="Register" ValidationGroup="RegisterGroup" runat="server" />

</fieldset>

</div>

</form>

In the second case, the same is required.

Note:

It is not mandatory to implement the ‘ValidationGroup’ concept in both the Divs. Implementing in only one will do. Similar is for User Controls.

Thanks & Regards,

Arun Manglick || Tech Lead

No comments:

Post a Comment