【问题标题】:ValidationSummary javascript errorValidationSummary javascript错误
【发布时间】:2012-02-13 17:15:54
【问题描述】:

我在 IE8 中的这行 javascript 中遇到错误。当 ValidationSummary 被注释掉时,它不会发生。我相信这是控件生成的代码。

ValidationSummary 位于 asp.net 的内容页面中使用的 UserControl 上。

当我使用 IE 开发者工具时,它会高亮显示这段代码

document.getElementById('ctl00_ctl00_body_pageBody_ucCC1_valSummary').dispose = function() {
    Array.remove(Page_ValidationSummaries, document.getElementById('ctl00_ctl00_body_pageBody_ucCC1_valSummary'));
}
(function() {var fn = function() {Sys.Extended.UI.ModalPopupBehavior.invokeViaServer('ctl00_ctl00_body_pageBody_mdlPopupExtender', true); Sys.Application.remove_load(fn);};Sys.Application.add_load(fn);})()




<asp:ValidationSummary 
runat="server" 
ID="valSummary" 
ShowSummary="true" 
DisplayMode="BulletList"
CssClass="summaryValidation" 
HeaderText="Errors:" 
ForeColor="White" 
ValidationGroup="VldGrpHospital" />

【问题讨论】:

  • @GrailsGuy(可能还有其他人),我猜测类似于 TypeError: (intermediate value)(...) is not a function。或者至少这就是我通过拥有一个 ASP.NET ValidationSummary 控件并尝试显示一个 ModalPopupExtender 控件(Ajax 控件工具包)所经历的。

标签: javascript asp.net validationsummary


【解决方案1】:

原来这是 ajax 控制工具包中的一个已知错误。他们声称它已在最新版本中修复,但我认为没有。解决方法是创建一个继承自验证摘要的服务器控件,并在两个 javascript 语句之间插入一个缺少的分号。

http://ajaxcontroltoolkit.codeplex.com/workitem/27024

[ToolboxData("")]
public class AjaxValidationSummary : ValidationSummary
{
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), this.ClientID, ";", true);
    }
}

【讨论】:

    【解决方案2】:

    接受的答案虽然可能有效,但可能不是最佳解决方案,因为它依赖于脚本块注册顺序匹配输出顺序,这不是一件好事。来自RegisterStartupScript 的 MSDN 页面:

    使用 RegisterStartupScript 注册的启动脚本块不能保证按照注册的顺序输出。如果启动脚本块的顺序很重要,请使用 StringBuilder 对象将脚本块收集到单个字符串中,然后将它们全部注册为单个启动脚本。

    这是一个可能更好的解决方法:

    public class ValidationSummarySansBug : ValidationSummary
    {
        // The bug is that the base class OnPreRender renders some javascript without a semicolon.
        // This solution registers an almost-identical script *with* a semicolon using the same type and key and relies on the
        // behavior of ClientScriptManager.RegisterStartupScript to ignore duplicate script registrations for the same type/key
        protected override void OnPreRender(EventArgs e)
        {
            if (Enabled)
            {
                ScriptManager.RegisterStartupScript(
                    this,
                    typeof(ValidationSummary), // this type must match the base type's specified type for the script we're fixing
                    ClientID + "_DisposeScript", // this key must match the base type key for the script we're fixing
                    @"
    document.getElementById('{0}').dispose = function() {{
        Array.remove(Page_ValidationSummaries, document.getElementById('{0}'));
    }};
                ".FormatInvariant(ClientID),
                    true);
            }
    
            base.OnPreRender(e);
        }
    }
    

    【讨论】:

      【解决方案3】:

      此错误是 ASP.NET 中验证控件的一部分,而不是 AJAX 工具包的一部分。您可以使用EnableClientScript="false" 关闭页面上所有验证控件的客户端验证,错误就会消失。

      【讨论】:

      • 我不确定,除非我们谈论的是完全不同的错误。当我使用旧版本的 Ajax Control Toolkit 进行测试时,我没有收到 [same] JavaScript 错误。不过,在控件上设置 EnableClientScript="False" 确实有效。
      【解决方案4】:

      我在 Windows 7 上遇到了同样的问题。这个问题的原因是没有当前的 Windows 更新(可能 .NET 已过时)。 (我已经关闭了自动更新)。安装更新后问题解决了

      【讨论】:

        【解决方案5】:

        遇到同样的问题,但出于完全不同的原因,有以下代码:

        <% if(showvalidators){ %>
        <tr>
            <td>
            <asp:ValidationSummary ID="SummaryValidator" runat="server" ForeColor="Red" EnableClientScript="true" DisplayMode="BulletList" ShowMessageBox="false" HeaderText="" />
            </td>
        </tr>
        <%}%>
        

        如果 showvalidator 为 false,我必须明确禁用 ValidationSummaryControl 服务器端。 Javascriptvalidationcode 尝试查找摘要控件 (getelementbyid) 但未在页面中呈现。 Enableclientsidescript="false" 解决了这个问题,因为不再有 javascriptcode 正在寻找丢失的控件。我想这种行为是在 .NET 4.5 中针对 ValidationSummaryControl 解决的,所以问题只发生在 .NET 4.0

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-06-04
          • 1970-01-01
          • 2011-09-19
          • 1970-01-01
          • 1970-01-01
          • 2010-11-28
          • 1970-01-01
          相关资源
          最近更新 更多