2010-03-17 51 views
0

我正在使用AJAX Cascading下拉列表,但想要添加事件驗證,例如比較驗證器。如何驗證AJAX級聯下拉列表

由於級聯下拉列表要求頁面事件驗證被禁用,最好的方法是進行驗證?

感謝

安迪

驗證嘗試: 我曾嘗試使用它調用一個JavaScript函數的自定義的驗證,但它似乎是撿了控制。我收到以下錯誤Microsoft JScript runtime error: Object required

function ValidateCostCentCat(source, arguments) 
{ 
    var countryList = document.getElementById("ddlCategory"); 
    if (null != countryList) 
    { 
    var iValue = countryList.options[countryList.selectedIndex].value; 
    if (iValue == "Select Category") 
    { 
     arguments.IsValid = true; 
    } 
    else 
    { 
     arguments.IsValid = false; 
    } 
    } 
} 

的成本加幅爲自定義的驗證是

<asp:CustomValidator ID="valcustCategory" runat="server" CssClass="error" Display="Dynamic" ValidationGroup="DirectHire" ClientValidationFunction="ValidateCostCentCat" 
      ErrorMessage="Please select a Cost Centre Category from the drop down list provided.">!</asp:CustomValidator> 

回答

3

閱讀本:http://www.w3schools.com/PHP/php_ajax_database.asp

的例子demostrate如何選擇 值從下拉框通過 AJAX發送並獲取回覆!

在中間你可以做所有的 你想要的驗證!

已更新代碼只是爲了好玩! ;-)

假設你的選擇是

<asp:DropDownList ID="CategoryDropDownList" runat="server"> 

那麼你的功能是這樣的:

function ValidateCostCentCat(source, arguments) 
{ 
    var countryList = document.getElementById("CategoryDropDownList"); 
    if (null != countryList) 
    { 

    var iValue = countryList.options[countryList.selectedIndex].value; 

    if (iValue == "Select Category") { 

    arguments.IsValid = true; 

    } else { 

    arguments.IsValid = false; 

    } 
    } 
} 

這必須按預期工作!

希望對您有所幫助!

+0

我已經嘗試使用客戶端驗證自定義驗證控件,但我不能讓它看到下拉列表。我在上述問題的底部發布了我的代碼。 謝謝 – anD666 2010-03-18 11:30:39

+0

查看更新! – 2010-03-18 12:02:57

+0

我已更新以適合您的代碼!讓我知道! – 2010-03-18 12:23:10