Team LiB
Previous Section Next Section

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 id="TextBox1" runat="server" validationgroup="firstform">
            </asp:textbox>
            <asp:requiredfieldvalidator id="RequiredFieldValidator1"
                runat="server" display="Dynamic"
                setfocusonerror="True"
                validationgroup="firstform"
                controltovalidate="TextBox1">***</asp:requiredfieldvalidator>
            <br />
            <br />
            <asp:button id="Button1" runat="server"
                validationgroup="firstform" text="Submit" />
        </p>


        <h1>Second form</h1>
        <p>
            <asp:textbox id="TextBox2" runat="server" validationgroup="secondform">
            </asp:textbox>
            <asp:requiredfieldvalidator id="RequiredFieldValidator2" runat="server"
                display="Dynamic"
                setfocusonerror="True"
                    validationgroup="secondform"
                    controltovalidate="TextBox2">***</asp:requiredfieldvalidator>
            <br />
            <br />
            <asp:button id="Button2" runat="server"
                validationgroup="secondform"
                text="Submit" />
        </p>

    </form>
</body>
</html>
End example

Team LiB
Previous Section Next Section