How do I keep ASP.NET client-side validation from firing before a postback attempt?

Posted on 2007-08-12
Last Modified: 2013-11-25
I've created a CustomValidator in ASP.NET that does client-side validation on a DropDownList control (actually, it's an extended class, but with very little additional functionality).  The validator works well, except for the fact that it fires as soon as I change the selection in the list and I would like it to wait until I submit the entire form.

I've set the CausesValidation property to false on the dropdown but it still validates.  Below are some code snippets to give you an idea:

----------- CUSTOM DROPDOWNLIST -----------------------------------------------
<armadillo:ShippingPackageComboBox ID="cboPackage"
        InitialItem="-- Select Package --"  />

----------- CUSTOM VALIDATOR ------------------------------------------------------
<asp:CustomValidator ID="validatorPackageCustom"
      ErrorMessage = ""

---------- JAVASCRIPT VALIDATION FUNCTION ---------------------------------------
function validatePackage(sender, args)
            var cboPackage = document.getElementById('<%= cboPackage.ClientID %>');  
            var valPackage = document.getElementById('<%= validatorPackageCustom.ClientID %>');  
            var radCalculatedRate = document.getElementById('<%= radCalculatedRate.ClientID %>');
            var txtWeight = document.getElementById('<%= txtWeight.ClientID %>');
            var txtLength = document.getElementById('<%= txtLength.ClientID %>');
            var txtWidth = document.getElementById('<%= txtWidth.ClientID %>');
            var txtHeight = document.getElementById('<%= txtHeight.ClientID %>');
            // Only validate if this is a calculated rate
            if(radCalculatedRate.checked) {
                weightIsValid = isNumeric(txtWeight.value) && txtWeight.value > 0;
                dimensionIsValid = txtLength.value != '' && isNumeric(txtLength.value) && txtLength.value > 0 &&
                                   txtWidth.value != '' && isNumeric(txtWidth.value) && txtWidth.value > 0 &&
                                   txtHeight.value != '' && isNumeric(txtHeight.value) && txtHeight.value > 0;
                if(cboPackage.selectedIndex == 0) {
                    if (weightIsValid) {
                        valPackage.innerText = 'Please select a package type.';
                    } else {
                        valPackage.innerText = 'Please select a package type and enter a valid weight';
                    args.IsValid = false;
                } else if(cboPackage.options[cboPackage.selectedIndex].innerText == 'My Packaging'){
                    if (weightIsValid && dimensionIsValid) {
                        args.IsValid = true;
                    } else if (weightIsValid) {
                        valPackage.innerText = 'Please enter a valid package dimension.';
                        args.IsValid = false;
                    } else if (dimensionIsValid) {
                        valPackage.innerText = 'Please enter a valid package weight.';
                        args.IsValid = false;
                    } else {
                        valPackage.innerText = 'Please enter a valid package weight and dimension.';
                        args.IsValid = false;
                } else { // Standard UPS packaging types = everything else
                    if (weightIsValid) {
                        args.IsValid = true;
                    } else {
                        valPackage.innerText = 'Please enter a valid weight.';
                        args.IsValid = false;

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using armadillo.util;
using armadillo;

/// <summary>
/// Standard DropDownList populated with the appropriate shipping service types
/// </summary>

namespace armadillo

    [ToolboxData(@"<{0}:ShippingPackageComboBox runat=""server"" CssClass=""standardTextField"" InitialItem=""-- Select --"" \>")]
    public class ShippingPackageComboBox : DropDownList

        private string m_initialItemText = "";

        protected override void OnLoad(EventArgs e)
            if (this.Items.Count == 0)

        /// <summary>
        /// Gets the appropriate selections from the database and populates the list
        /// </summary>
        protected void initShippingPackages()
            SqlConnection connection = SQLUtils.openConnection();
            SqlCommand command = new SqlCommand(SQLUtils.SQL_LISTSHIPPINGPACKAGES, connection);
            DataSet ds = SQLUtils.retrieveDataSet(command, "ShippingPackageType", "TBL_SHIPPING_PACKAGE_TYPE");

            // Populate drop-down
            this.Items.Add(new ListItem(m_initialItemText, "0"));
            for (int i = 0; i < ds.Tables["TBL_SHIPPING_PACKAGE_TYPE"].Rows.Count; i++)
                string text = ds.Tables["TBL_SHIPPING_PACKAGE_TYPE"].Rows[i]["NAME"].ToString();
                string value = ds.Tables["TBL_SHIPPING_PACKAGE_TYPE"].Rows[i]["ID"].ToString();
                this.Items.Add(new ListItem(text, value));

        public string InitialItem
            get { return m_initialItemText; }
            set { m_initialItemText = value; }

This isn't a show-stopper as the validation still works but it is really annoying :) and I am really curious to know why this won't work!  Any ideas?

Question by:moshecristel
    LVL 4

    Expert Comment

    what happens if you turn off client-side validation for the control (EnableClientScript = false)?  

    Author Comment

    It was a good idea, but if I set EnableClientScript="false", the validation never fires at all--Neither on change of the drop-down list or on attempted PostBack.

    To clarify, the reason I want to suppress this "onChange" validation is because the drop down list is one of several that I want to be validated at the same time (a custom multi-control validation).  That is, the user will have to enter a:

    Package Type (the drop-down list)
    Weight (1 Text Field)
    Dimension (3 Text Fields: Length, Width and Height)

    With the current behavior, the user would first select a package type and before having a chance to enter the second item (weight), the validation would fire and display the "error message" saying that a weight and dimension are required.  Technically, this shouldn't be an error--they just haven't had a chance to enter everything yet :)

    Maybe I'm approaching this all wrong.  Any other ideas?
    LVL 12

    Accepted Solution

    CausesValidation="false" on an element will allow it to fire back but not fire the validation controls.

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde… provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
    Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now