Anything New for Validation Controls?


There is really no big news regarding validation controls, but there are two really useful small items of interest.

First, you can define several validation groups on one page to allow the validation of several independent forms, for example. To define a group, you have to set the ValidationGroup property of all participating controls (input fields, validation controls, and buttons) to an identical unique value—for example, "ContactForm." Everything else is done by the Framework, on the client side as well as on the server side. The second feature is a nice goodie that ensures that the first invalid input field automatically gets the focus.

Both features are included in Listing 11-14. Two identical forms are placed here on a single page. Both are validated independently according to which button is clicked. Figure 11-13 shows the output.

click to expand
Figure 11-13: Validation groups are definitely a missing feature of version 1.0.

Listing 11-14: Using Validation Groups to Create Several "Virtual" Forms

start example
 <%@ page language="C#" %> <script runat="server"> </script> <html> <head runat="server">     <title>Untitled Page</title> </head> <body>     <form runat="server">         <h1>First form</h1>         <p>             <asp:textbox  runat="server" validationgroup="firstform">             </asp:textbox>             <asp:requiredfieldvalidator                  runat="server" display="Dynamic"                 setfocusonerror="True"                 validationgroup="firstform"                 controltovalidate="TextBox1">***</asp:requiredfieldvalidator>             <br />             <br />             <asp:button  runat="server"                 validationgroup="firstform" text="Submit" />         </p>         <h1>Second form</h1>         <p>             <asp:textbox  runat="server" validationgroup="secondform">             </asp:textbox>             <asp:requiredfieldvalidator  runat="server"                 display="Dynamic"                 setfocusonerror="True"                     validationgroup="secondform"                     controltovalidate="TextBox2">***</asp:requiredfieldvalidator>             <br />             <br />             <asp:button  runat="server"                 validationgroup="secondform"                 text="Submit" />         </p>     </form> </body> </html> 
end example




ASP. NET 2.0 Revealed
ASP.NET 2.0 Revealed
ISBN: 1590593375
EAN: 2147483647
Year: 2005
Pages: 133

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net