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

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"
        runat="server"
        CausesValidation="false"
        CssClass="standardTextField"
        InitialItem="-- Select Package --"  />

----------- CUSTOM VALIDATOR ------------------------------------------------------
<asp:CustomValidator ID="validatorPackageCustom"
      runat="server"
      ControlToValidate="cboPackage"
      ErrorMessage = ""
      Display="dynamic"
      CssClass="validationText"
      ClientValidationFunction="validatePackage"
      SetFocusOnError="true"/>

---------- 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;
                    }
                }
            }
}

---------- EXTENDED DROPDOWNLIST CODE (NOT SURE THIS WILL BE HELPFUL) ------------------------
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 --"" \>")]
    [ValidationProperty("SelectedIndex")]
    public class ShippingPackageComboBox : DropDownList
    {

        private string m_initialItemText = "";

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (this.Items.Count == 0)
                initShippingPackages();
        }

        /// <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));
            }
            connection.Close();
        }

        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?

Thanks!
moshecristelAsked:
Who is Participating?
 
lunadlConnect With a Mentor Commented:
CausesValidation="false" on an element will allow it to fire back but not fire the validation controls.
0
 
davehunt00Commented:
what happens if you turn off client-side validation for the control (EnableClientScript = false)?  
0
 
moshecristelAuthor Commented:
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?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.