INFO: ASP.NET ValidationSummary Web Form Control

Article translations Article translations
Article ID: 316712 - View products that this article applies to.
This article was previously published under Q316712
Expand all | Collapse all

SUMMARY

There are six Web server controls that you can use to validate input on your Web Form pages. This article discusses the ValidationSummary control.

MORE INFORMATION

The ValidationSummary control is used to summarize the error messages from all validators on a Web page in a single location. The summary can be displayed as a list, a bulleted list, or as a single paragraph based on the DisplayMode property. The summary can be displayed on the Web page and in a message box when you set the ShowSummary and ShowMessageBox properties accordingly.

The following sample code demonstrates uses of the RequiredFieldValidator, RangeValidator, and ValidationSummary controls. If a value is not entered in the Name, Age, or Height text boxes, or if a valid height is not entered in the Height text box, the ValidationSummary control lists a summary of the validation errors.

NOTE: When you set the ShowMessageBox value to True on the ValidationSummary control, the summary is displayed in a client-side message box.
<html>
	<body>
	    <form runat=server id=form1>
				
	       <asp:Label ID="lblName" Runat=server>First Name:</asp:Label>
                <asp:TextBox ID="txtName" Runat=server></asp:TextBox>			
                <asp:RequiredFieldValidator 
                    id="RequiredFieldValidatorName"
                    ControlToValidate="txtName" 
                    ErrorMessage="First Name field is empty"
                    Display="Static"
                    InitialValue="" Width="100%" runat=server>
                </asp:RequiredFieldValidator><br>

	       <asp:Label ID="lblAge" Runat=server>Age:</asp:Label>
	       <asp:TextBox ID="txtAge" Runat=server></asp:TextBox>			
                <asp:RequiredFieldValidator 
                    id="RequiredfieldvalidatorAge"
                    ControlToValidate="txtAge" 
                    ErrorMessage="Age field is empty"
                    Display="Static"
                    InitialValue="" Width="100%" runat=server>
                </asp:RequiredFieldValidator><br>

               <asp:Label ID="lblHeight" Runat=server>Height:</asp:Label>
	      <asp:TextBox ID="txtHeight" Runat=server></asp:TextBox>			
		<asp:RequiredFieldValidator 
                    id="RequiredfieldvalidatorHeight"
                    ControlToValidate="txtHeight" 
                    ErrorMessage="Height field is empty"
                    Display="Static"
                    InitialValue="" Width="100%" runat=server>
                </asp:RequiredFieldValidator>
			
               <asp:RangeValidator 
                   id="RangeValidatorHeight"
                   ControlToValidate="txtHeight" 
                   ErrorMessage="Incorrect value for the height field"
                   Display="Static"
                   Type="Integer"
		 MinimumValue="1"
		 MaximumValue="10"                
                   Width="100%" runat=server>
              </asp:RangeValidator><br>

              <asp:ValidationSummary 
                  id="valSum" 
                  DisplayMode="BulletList" 
                  runat="server"
                  HeaderText="Summary of Validation Errors:"
                  Font-Name="verdana"                 
                  Font-Size="12"/><br>
                
              <asp:Button id=btnValidate text="Validate" runat=server />
	   </form>
	</body>
</html>
				

REFERENCES

For additional information about Validation controls, browse to the following MSDN Web sites:
Validation Server Controls
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconaspnetsyntaxforvalidationcontrols.asp?frame=true
ValidationSummary Control
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconvalidationsummarycontrol.asp?frame=true

Properties

Article ID: 316712 - Last Review: February 23, 2007 - Revision: 3.4
APPLIES TO
  • Microsoft ASP.NET 1.1
  • Microsoft ASP.NET 1.0
Keywords: 
kbinfo kbservercontrols kbvalidation kbwebforms KB316712

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com