Home ASP.NET Hosting Dedicated Servers Contact Us
Announcements
Virtual Tiers
.NET Applications
XML Web Services
SQL Server Database
Web Traffic Statistics
Much More...
ASP.NET Web Site Hosting
Dedicated Servers
Windows 2003 Server
2.4 GHz Pentium 4
1024 MB RAM
80 GB Hard Drive
1000 GB/month
Fully Managed
Free Setup!
$268.00/month
Windows Dedicated Servers
Specialized Plans
10 or more Domains
Windows Services
Custom Plans
Windows Services
Learning & Support
About Us



Form Validation Controls



Learn More about Server Intellect



The ASP.NET Web Forms page framework provides a set of validation server controls that provide an easy-to-use but powerful way to check input forms for errors, and, if necessary, display messages to the user.

Validation controls are added to an ASP.NET page like other server controls. There are controls for specific types of validation, such as range checking or pattern matching, plus a RequiredFieldValidator that ensures that a user does not skip an entry field.

The following example demonstrates how to use two <asp:requiredfieldvalidator runat=server> controls on a page to validate the contents of the TextBox and DropDownList controls.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>
<head>
</head>

<script language="VB" runat=server>

Sub SubmitBtn_Click(Sender As Object, E As EventArgs)

Dim DS As DataSet
Dim MyConnection As SqlConnection
Dim MyCommand As SqlDataAdapter

MyConnection = New SqlConnection( "server=(local)\NetSDK;database=pubs;Trusted_Connection=yes")
MyCommand = New SqlDataAdapter("select * from Titles where type='" + Category.SelectedItem.Value + "'", myConnection)

DS = new DataSet()
MyCommand.Fill(DS, "Titles")

MyList.DataSource = DS.Tables("Titles").DefaultView
MyList.DataBind()

End Sub

</script>

<body>

<center>

<form action="form.aspx" method="post" runat="server">

<asp:adrotator AdvertisementFile="ads.xml" BorderColor="black" BorderWidth=1 runat="server"/>

<table>
<tr>
<td> Name: </td>
<td> <asp:textbox id="Name" runat="server"/> </td>
<td> <asp:RequiredFieldValidator ControlToValidate="Name" Display="Dynamic" errormessage="You must enter your name!" runat=server/> </td>
</tr>
<tr>
<td> Category: </td>
<td>
<asp:dropdownlist id="Category" width=147 runat=server>
<asp:listitem><!--Select Category--></asp:listitem>
<asp:listitem >psychology</asp:listitem>
<asp:listitem >business</asp:listitem>
<asp:listitem >popular_comp</asp:listitem>
</asp:dropdownlist>
</td>
<td> <asp:RequiredFieldValidator ControlToValidate="Category" Display="Dynamic" InitialValue="<!--Select Category-->" errormessage="You must select a category!" runat=server/> </td>
</tr>
<tr>
<td></td>
<td><asp:button text="Lookup" OnClick="SubmitBtn_Click" runat="server"/></td>
</tr>
</table>

<p>

<asp:datalist id="MyList" repeatcolumns="2" borderwidth="0" runat="server">

<ItemTemplate>

<table>
<tr>

<td>
<img src='<%# DataBinder.Eval(Container.DataItem, "title_id", "/quickstart/aspplus/images/title-{0}.gif") %>'>
</td>

<td width=250 valign=top>

<b><%# DataBinder.Eval(Container.DataItem, "title") %></b>

<br><br>

Price: <%# DataBinder.Eval(Container.DataItem, "price", "${0}") %>
</td>

</tr>
</table>

</ItemTemplate>

</asp:datalist>

</form>

</center>

</body>

</html>


ads.xml

<Advertisements>

<Ad>
<ImageUrl>banner1.gif</ImageUrl>
<NavigateUrl>http://www.microsoft.com</NavigateUrl>
<AlternateText>Alt Text</AlternateText>
<Keyword>Computers</Keyword>
<Impressions>80</Impressions>
</Ad>

<Ad>
<ImageUrl>banner2.gif</ImageUrl>
<NavigateUrl>http://www.microsoft.com</NavigateUrl>
<AlternateText>Alt Text</AlternateText>
<Keyword>Computers</Keyword>
<Impressions>80</Impressions>
</Ad>

<Ad>
<ImageUrl>banner3.gif</ImageUrl>
<NavigateUrl>http://www.microsoft.com</NavigateUrl>
<AlternateText>Alt Text</AlternateText>
<Keyword>Computers</Keyword>
<Impressions>80</Impressions>
</Ad>

</Advertisements>


Note that the validation controls have both uplevel and downlevel client support. Uplevel browsers perform validation on the client (using JavaScript and DHTML) and on the server. Downlevel browsers perform the validation only on the server. The programming model for the two scenarios is identical.

Note that ASP.NET page developers can optionally check the Page.IsValid property at run time to determine whether all validation server controls on a page are currently valid. This provides a simple way to determine whether or not to proceed with business logic. For example, the following sample performs a Page.IsValid check before executing a database lookup on the specified category.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>
<head>
</head>

<script language="VB" runat=server>

Sub SubmitBtn_Click(Sender As Object, E As EventArgs)

If (Page.IsValid)

Dim DS As DataSet
Dim MyConnection As SqlConnection
Dim MyCommand As SqlDataAdapter

MyConnection = New SqlConnection( "server=(local)\NetSDK;database=pubs;Trusted_Connection=yes")
MyCommand = New SqlDataAdapter("select * from Titles where type='" + Category.SelectedItem.Value + "'", myConnection)

DS = new DataSet()
MyCommand.Fill(DS, "Titles")

MyList.DataSource = DS.Tables("Titles").DefaultView
MyList.DataBind()

End If

End Sub

</script>

<body>

<center>

<form action="form.aspx" method="post" runat="server">

<asp:adrotator AdvertisementFile="ads.xml" BorderColor="black" BorderWidth=1 runat="server"/>

<table>
<tr>
<td> Name: </td>
<td> <asp:textbox id="Name" runat="server"/> </td>
<td> <asp:RequiredFieldValidator ControlToValidate="Name" Display="Dynamic" errormessage="You must enter your name!" runat=server/> </td>
</tr>
<tr>
<td> Category: </td>
<td>
<asp:dropdownlist id="Category" width=147 runat=server>
<asp:listitem><!--Select Category--></asp:listitem>
<asp:listitem >psychology</asp:listitem>
<asp:listitem >business</asp:listitem>
<asp:listitem >popular_comp</asp:listitem>
</asp:dropdownlist>
</td>
<td> <asp:RequiredFieldValidator ControlToValidate="Category" Display="Dynamic" InitialValue="<!--Select Category-->" errormessage="You must select a category!" runat=server/> </td>
</tr>
<tr>
<td></td>
<td><asp:button text="Lookup" OnClick="SubmitBtn_Click" runat="server"/></td>
</tr>
</table>

<p>

<asp:datalist id="MyList" repeatcolumns="2" borderwidth="0" runat="server">

<ItemTemplate>

<table>
<tr>

<td>
<img src='<%# DataBinder.Eval(Container.DataItem, "title_id", "/quickstart/aspplus/images/title-{0}.gif") %>'>
</td>

<td width=250 valign=top>

<b><%# DataBinder.Eval(Container.DataItem, "title") %></b>

<br><br>

Price: <%# DataBinder.Eval(Container.DataItem, "price", "${0}") %>
</td>

</tr>
</table>

</ItemTemplate>

</asp:datalist>

</form>

</center>

</body>

</html>


ads.xml

<Advertisements>

<Ad>
<ImageUrl>banner1.gif</ImageUrl>
<NavigateUrl>http://www.microsoft.com</NavigateUrl>
<AlternateText>Alt Text</AlternateText>
<Keyword>Computers</Keyword>
<Impressions>80</Impressions>
</Ad>

<Ad>
<ImageUrl>banner2.gif</ImageUrl>
<NavigateUrl>http://www.microsoft.com</NavigateUrl>
<AlternateText>Alt Text</AlternateText>
<Keyword>Computers</Keyword>
<Impressions>80</Impressions>
</Ad>

<Ad>
<ImageUrl>banner3.gif</ImageUrl>
<NavigateUrl>http://www.microsoft.com</NavigateUrl>
<AlternateText>Alt Text</AlternateText>
<Keyword>Computers</Keyword>
<Impressions>80</Impressions>
</Ad>

</Advertisements>