Archive, ASP.NET
     

Validating Drop Downs In ASP.NET – Part 1

User input validation is an important component in any user interface. In the realm of web development a response UI is extremely important to the users experience.

If you are like myself, programming in JavaScript does not come easily to you. Fortunately, ASP.NET contains several validator controls that allow for client side form validation without you having to program any JavaScript.

Take for example a simple drop down control.

<asp:DropDownList ID="ddlSampleDropDown" runat="server">
	<asp:ListItem Value="">--please select one--</asp:ListItem>
	<asp:ListItem Value="1">Apples</asp:ListItem>
	<asp:ListItem Value="2">oranges</asp:ListItem>
</asp:DropDownList>

In our scenario we do not want to pre-select a choice for the user, but we do want the user to have to make a choice. Instead of posting the form back to the server to validate the drop down, we can make use of the Required Field Validator control.

The Required Field Validator is an ASP.NET control that prevents the form from posting back until another control has been modified from its initial value.

 

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
	ControlToValidate="ddlSampleDropDown" InitialValue=""
	ErrorMessage="Please make a choice">

The important parts of the validator are the ControlToValidate, InitialValue, and ErrorMessage properties.

The ControlToValidate is the ASP.NET control id that we need to validate, so in our example the id of the drop down. The InitialValue for our example is the value of the ListItem of our drop down that represents the unselected status. And finally the ErrorMessage property contains literal text to be displayed to your site visitor if they do not select a valid value before attempting to post the form.

The RequiredFieldValidator control does not remove the need for server side validation of form input, it just lessons the burden of providing responsive error messages.

Never miss an article! Subscribe to my newsletter and I'll keep you updated with the latest content.

 

About Jason

Jason is an experienced entrepreneur & software developer skilled in leadership, mobile development, data synchronization, and SaaS architecture. He earned his Bachelor of Science (B.S.) in Computer Science from Arkansas State University.
View all posts by Jason →

Leave a Reply

Your email address will not be published. Required fields are marked *