ascx page 中的 javascript 抛出 HTTPexception

javascript in ascx page throwing HTTPexception

本文关键字:抛出 HTTPexception javascript 中的 page ascx      更新时间:2023-09-26
<script type="text/javascript">
function ValidateProductID(sender, args)
{
    var productID = document.getElementById('<%=txtProductID.ClientID%>').value;
    var productType = document.getElementById('<%=rcbProduct.ClientID%>').value;
    if (productID != "" && productType == "") {
        args.IsValid = false;
    }
    else
        args.IsValid = true;
}

自定义验证程序的此脚本引发异常。

An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code Additional information: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>) .

有人可以告诉我如何解决它。

ClientIDMode="Static"添加到这些文本框(或任何文本框中(,然后只使用document.getElementById('txtProductID').value;

根据文档,将ClientIDMode设置为 Static 意味着:

"客户端 ID"值设置为 ID 属性的值。

这意味着 ID 不会被某些算法更改,并且会被txtProductID,而不是您必须使用 txtProductID.ClientID 访问的内容。

通常,您遇到的错误是因为您将该脚本放在其他带有runat="server"的控件中。上面的解决方案可能是您最简单的解决方案,尽管如果没有更多的代码,很难说。

显然,我将脚本从内容模板中取出,将其与现有代码一起移入,它可以工作!