首页 > 解决方案 > ReportViewer 15.0.0 渲染:Sys.ArgumentNullException:值不能为空。参数名称:元素

问题描述

我在使用 MS ReportViewer 呈现报告(具有特定参数集)时遇到以下问题。从内部 ReportViewer JS 代码调用的 JS 失败,特别是“ScriptResource.axd”的这一部分:

var $addHandlers = Sys.UI.DomEvent.addHandlers = function Sys$UI$DomEvent$addHandlers(element, events, handlerOwner, autoRemove) {
    /// <summary locid="M:J#Sys.UI.DomEvent.addHandlers" />
    /// <param name="element"></param>
    /// <param name="events" type="Object"></param>
    /// <param name="handlerOwner" optional="true"></param>
    /// <param name="autoRemove" type="Boolean" optional="true"></param>
    var e = Function._validateParams(arguments, [
        {name: "element"},
        {name: "events", type: Object},
        {name: "handlerOwner", optional: true},
        {name: "autoRemove", type: Boolean, optional: true}
    ]);
    if (e) throw e;
    Sys.UI.DomEvent._ensureDomNode(element);
    for (var name in events) {
        var handler = events[name];
        if (typeof(handler) !== 'function') throw Error.invalidOperation(Sys.Res.cantAddNonFunctionhandler);
        if (handlerOwner) {
            handler = Function.createDelegate(handlerOwner, handler);
        }
        $addHandler(element, name, handler, autoRemove || false);
    }
}

“Sys.UI.DomEvent._ensureDomNode(element);” 行失败并出现以下错误:

Sys.ArgumentNullException:值不能为空。参数名称:元素

当我查找堆栈跟踪时:

在此处输入图像描述

看来这个问题的来源是从主“报告”页面调用的 JS:

Sys.Application.add_init(function() {
    $create(Microsoft.Reporting.WebFormsClient._Splitter, {"HoverStyle":"SplitterHover","ImageCollapse":"/Reserved.ReportViewerWebControl.axd?OpType=Resource\u0026Version=15.0.900.148\u0026Name=Microsoft.Reporting.WebForms.Icons.SplitterHorizCollapse.png","ImageCollapseHover":"/Reserved.ReportViewerWebControl.axd?OpType=Resource\u0026Version=15.0.900.148\u0026Name=Microsoft.Reporting.WebForms.Icons.SplitterHorizCollapseHover.png","ImageExpand":"/Reserved.ReportViewerWebControl.axd?OpType=Resource\u0026Version=15.0.900.148\u0026Name=Microsoft.Reporting.WebForms.Icons.SplitterHorizExpand.png","ImageExpandHover":"/Reserved.ReportViewerWebControl.axd?OpType=Resource\u0026Version=15.0.900.148\u0026Name=Microsoft.Reporting.WebForms.Icons.SplitterHorizExpandHover.png","ImageId":"ctl00_ApplicationBody_rvReport_ToggleParam_img","IsCollapsable":true,"NormalStyle":"SplitterNormal","Resizable":false,"StoreCollapseField":"ctl00_ApplicationBody_rvReport_ToggleParam_collapse","StorePositionField":"ctl00_ApplicationBody_rvReport_ToggleParam_store","TooltipCollapse":"Hide Parameters","TooltipExpand":"Show Parameters","Vertical":false}, null, null, $get("ctl00_ApplicationBody_rvReport_ToggleParam"));
});

这破坏了呈现的报告。我不确定如何进一步追踪这一点,我知道您可以使用不同的参数格式化报告,但我不明白如何调试 ReportViewer 库内部的缩小 JS。

这个 JS 失败是某些报告的已知问题吗?我正在运行最新版本的库(15.0.0)。我会发布报告和参数,但是它们包含敏感信息。如何调试 ReportViewer 库内部的问题以解决诸如此类的问题?

标签: javascriptwebformsreportviewer

解决方案


非常棘手,我在 MasterPage 中运行了一个 C# 方法,禁用某些类型的控件,以便用户无法“编辑”页面,看起来像这样:

//CommonFunctions
public static List<T> GetAllControlsRecursiveByType<T>(ControlCollection Controls) where T : Control
{
    List<T> results = new List<T>();
    foreach (Control c in Controls)
    {
        if (c is T)
        {
            results.Add((T)c);
        }

        if (c.HasControls())
        {
            results.AddRange(GetAllControlsRecursiveByType<T>(c.Controls));
        }
    }
    return results;
}

public void DisableControls(Control control)
{
    if (control == null)
    {
        return;
    }

    DisableControl(control);
    foreach (System.Web.UI.Control c in control.Controls)
    {
        DisableControl(c);

        // Recurse into child controls.
        if (c.Controls.Count > 0)
        {
            DisableControls(c);
        }
    }
}

foreach (Control element in CommonFunctions.GetAllControlsRecursiveByType<Control>(FindControl("ApplicationBody").Controls))
{
    List<string> excludedIDs = new List<string>() { "btnAjaxDynamicFilterApplyFilter", "btnClose", "btnCancel", "btnExport" };
    List<Type> includedTypes = new List<Type>() { typeof(LinkButton), typeof(Button), typeof(ImageButton), typeof(Repeater), typeof(ABC.Controls.ABCRepeater),
        typeof(GridView), typeof(ABC.Controls.ABCGridView), typeof(ABC.Controls.ImageCheckBox) };

    if (!excludedIDs.Contains(element.ID) && includedTypes.Contains(element.GetType()))
    {
        DisableControls(element);
    }
}

原来这是在报表查看器中“禁用”某些呈现的控件,这反过来又破坏了前端的 JS。我通过从这个逻辑中排除“ReportViewer”来解决这个问题:

public bool ControlHasParentWithType(Control control, Type type)
{
    if (control == null || control.Parent == null)
    {
        return false;
    }
    else if (control.Parent.GetType() == type)
    {
        return true;
    }

    return ControlHasParentWithType(control.Parent, type);
}

//Within Method before disabling the control
if (ControlHasParentWithType(element, typeof(ReportViewer)))
{
    continue;
}

推荐阅读