Monday, February 14, 2005

[ASP.NET 2.0] Cool enhancement in Validation server control

Validation controls in ASP.NET 2.0 have a new attribute by name "ValidationGroup". This was on the wish list for quite sometime but I am really glad it was introduced in Whidbey. Using this we can easily group controls which needs to be fired on click of a button.

Just go through the below code snippet which I wrote to test this feature for your better understanding.

<body>
<form id="form1" runat="server">
<h1>GROUP I</h1>
Firstname:

<asp:TextBox ID="txtFirstname" Runat="server"></asp:TextBox>

<asp:Button ID="Button1" Runat="server" Text="Login" ValidationGroup="Group1" />

<asp:RequiredFieldValidator ID="RFV1" Runat="server" ErrorMessage="* Firstname is mandatory" ControlToValidate="txtFirstname" ValidationGroup="Group1">
</asp:RequiredFieldValidator>

<hr />


<h1>GROUP II</h1>
Email Address:
<asp:TextBox ID="txtEmail" Runat="server"></asp:TextBox>

<asp:Button ID="Button2" Runat="server" Text="Go" ValidationGroup="Group2" />

<asp:RegularExpressionValidator ID="RFV2" Runat="server" ErrorMessage="* Enter an valid email address!" ControlToValidate="txtEmail" ValidationGroup="Group2"> </asp:RegularExpressionValidator>

<asp:RequiredFieldValidator ID="RFV3" Runat="server" ErrorMessage="* Email address is mandatory" ControlToValidate="txtEmail" ValidationGroup="Group2"> </asp:RequiredFieldValidator>


</form>
</body>

No comments: