【问题标题】:How to go about validating AJAX cascading drop down lists如何验证 AJAX 级联下拉列表
【发布时间】:2010-03-17 13:21:37
【问题描述】:

我正在使用 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>

【问题讨论】:

    标签: asp.net ajax validation


    【解决方案1】:

    阅读: http://www.w3schools.com/PHP/php_ajax_database.asp

    这个例子演示了如何选择一个 下拉框中的值通过发送它 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;
    
        }
      }
    }
    

    这必须按预期工作!

    希望对您有所帮助!

    【讨论】:

    • 我已经尝试使用客户端验证的自定义验证器控件,但我无法让它看到下拉列表。我已经在上述问题的底部发布了我的代码。谢谢
    • 我已经尝试了你给出的两个例子,但都没有工作,它没有选择下拉列表。它没有什么特别之处,我只是使用一个 web 方法来填充它并使用 AJAX 级联下拉列表控件。对不起,我之前没有尝试过客户端验证或使用自定义验证器。谢谢
    • 我尝试注释掉检查 countryList 是否为 null 的条件语句,确认客户端脚本没有找到 ddlCategory
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多