Mega Code Archive

 
Categories / ASP.Net Tutorial / Validation
 

Validating Drop-Down Lists with the RequiredFieldValidator Control

<%@ Page Language="VB" %> <script runat="server">    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)         Label1.Text = "Page is valid!"    End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">     <title>RequiredFieldValidator</title> </head> <body>     <form id="form1" runat="server">     <div>         <asp:DropDownList id="DropDownList1" runat="server">            <asp:ListItem Selected="True">Select a profession</asp:ListItem>            <asp:ListItem>Programmer</asp:ListItem>            <asp:ListItem>Lawyer</asp:ListItem>            <asp:ListItem>Doctor</asp:ListItem>            <asp:ListItem>Artist</asp:ListItem>         </asp:DropDownList>         <asp:RequiredFieldValidator id="RequiredFieldValidator1"                                      runat="server"                                      ErrorMessage="Please make a selection"                                     ControlToValidate="DropDownList1"                                     InitialValue="Select a profession">         </asp:RequiredFieldValidator>         <br />         <asp:Button ID="Button1" Runat="server" Text="Submit"           OnClick="Button1_Click" />         <br />         <asp:Label ID="Label1" Runat="server"></asp:Label>         </div>     </form> </body> </html>