Adding Customer Validation to asp:DropDownList control

The default behavior of the ASP.NET drop down list is that it selects the first item in the list if items if none of the item are explicitly set to be selected.  However one of the very common requirements in any project was forcing the user to select an item from the available list of items in the drop down list and not always the first item.

For Ex, in one of the projects, it’s required that the user need to select a product type before moving ahead and here is how it can be enforced.


First add the following code in the ASP.NET Form to add a drop down list of products.

 <asp:DropDownList ID=”ProductType” runat=”server” CssClass=”DropDownList” AppendDataBoundItems =”true”>

 <asp:ListItem Text= “<None>” Value = “-999”></asp:ListItem></asp:DropDownList





The above code adds the first item of the list as “<None>” with a value of “-999”.  AppendDataBoundItems property was used to add this item before we can dynamically bind the data to the list

Page_Load of the Page Behind

Add the following code in the page behind to bind the data to this list.

SqlConnection Conn = new SqlConnection(ConnectionString);       System.Data.DataTable ResultsDataTable = new System.Data.DataTable();try{

    if (!string.IsNullOrEmpty(Gloabals.ConnectionString))




  SqlDataAdapter DBAdapter;


  DBAdapter = new SqlDataAdapter(QueryString, Gloabals.ConnectionString);

  DBAdapter.ReturnProviderSpecificTypes = true;



  ProductType.DataTextField = “PRODUCT_NAME”;

  ProductType.DataValueField = “PRODUCT_CODE”;

  ProductType.DataSource = ResultsDataTable;



catch (Exception ex)


  if (Conn.State == System.Data.ConnectionState.Open)






 Now let’s add a customer Validate to this drop down list to validate and make sure at least one of the valid items is selected.


Add the following Customer Validator and validation summary n the ASP.NET Form

<asp:CustomValidator ID=”CustomValidator1″ runat=”server” ControlToValidate=”ProductType” ErrorMessage=”Invalid Product Type “ ClientValidationFunction=”CheckProductType”>span style=”color:White”>.</span></asp:CustomValidator><asp:ValidationSummary ID=”ValidationSummary1″ runat=”server”/>




 Add the client side validation function in the form as follows:

function CheckProductType(source, arguments) {

 var v = document.getElementById(“ctl00_ContentPlaceHolder1_TabContainer2_TabPanel1_ProductType”);


if ( v != null)


             var iValue = newNumber(v[v.selectedIndex].value);        

                    arguments.IsValid=(iValue != “-999”); 



 Add a button to the Form

< asp:Button ID=”SubmitBtn” runat=”server”  Text=”Submit onclick=”Submit_Click” CausesValidation=”true” />

Setting the CausesValidation property to “true” forces the Page to validate all the validators on the page and fires the client side validate function, if the  selected option and have not selected anything, it displays an error message


One thought on “Adding Customer Validation to asp:DropDownList control

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s