带有更新面板asp.net的Crystal报表

时间:2013-11-14 11:36:42

标签: c# asp.net crystal-reports updatepanel

我正在开发一个asp.net网站。

在该网站中,我需要在水晶报告的帮助下显示报告。

HTML

<asp:UpdatePanel ID="uPnlMain" runat="server" UpdateMode="Conditional">
    <ContentTemplate> 
        <table>
            <tr>
                <td>
                    <asp:Button ID="btnSave" runat="server" 
                            CssClass="btn" OnClick="btnSave_Click" Text="Show"/>
                </td>
            </tr>
            <tr>
                <td>
                    <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />                
                </td>
            </tr>
        </table>
    </ContentTemplate>
</asp:UpdatePanel>

报告源代码

protected void btnSave_Click(object sender, EventArgs e)
{
        ReportDocument crystalReport = new ReportDocument();
        crystalReport.Load(Server.MapPath("Reports/crptBalance.rpt"));
        DSBalance dsCustomers = GetData(@"
DECLARE @DateFrom DATETIME;
DECLARE @p_Dt DATETIME;
SELECT @p_Dt = '2013-11-14 00:00:00';
SELECT TOP 1 @DateFrom = vdate FROM tblLedger WHERE fdocid = 1 AND vdate <= @p_Dt ORDER BY vdate DESC;
IF @DateFrom IS NULL BEGIN
    SELECT TOP 1 @DateFrom = OpBalDate FROM tblTrnsOpBalMaster WHERE OpBalDate <= @p_Dt ORDER BY OpBalDate DESC;
END
SELECT Y.Customer,
    CONVERT(VARCHAR,Y.Date,103) AS [Date],
    UPPER(Y.Description) AS Description,
    Y.[Due Days],
    Y.Debit,
    Y.Credit,
    CASE WHEN Y.[Balance] >= 0 THEN Y.[Balance] ELSE -1 * Y.[Balance] END AS [Balance],
    CASE WHEN Y.[Balance] >= 0 THEN 'DR' ELSE 'CR' END AS [TP] FROM 
(
    SELECT X.Customer,
        X.Date,
        X.Description,
        X.[Due Days],
        X.Debit,
        X.Credit,
        X.Debit - X.Credit AS [Balance] FROM 
    (
        SELECT CUS.nm + ',  ' + cus.[add] AS [Customer],
            vdate [Date],
            narration AS [Description],
            DATEDIFF(DAY, vdate, GETDATE()) AS [Due Days],
            CASE WHEN amttype = 'DR' THEN amt ELSE 0.0000 END AS Debit,
            CASE WHEN amttype = 'CR' THEN amt ELSE 0.0000 END AS Credit
        FROM tblledger LED
            LEFT OUTER JOIN tblCustomer CUS ON (LED.acid = CUS.id)
        WHERE acid IN (42,7) AND vdate >= @DateFrom AND vdate < @p_Dt 
    )X
)Y
ORDER BY Y.Customer,Y.Date");
        crystalReport.SetDataSource(dsCustomers);
        CrystalReportViewer1.ReportSource = crystalReport;
}

我的问题是,当我在更新面板中使用水晶报表查看器时,它不会在报表中显示任何数据。

我的意思是说它显示空白报告。

如果没有更新面板,它可以正常工作。

任何人都可以告诉我这是什么问题

2 个答案:

答案 0 :(得分:0)

我曾试图做同样的事情,但我做不到。我研究了很多,没有发现任何东西,所以我解决了另一种解决方案。

我创建了一个名为“Reposrts”的母版页,在jquery里面用于填充iframe,其中另一个页面是水晶报告。

像这样:

“Reports.aspx”

<div id="frameReport" style="display: none;"></div>
<script type="text/javascript">
    $(document).ready(function () {

        $("a.lnkReportMenu").click(function (e) {
            var idReport = $(this).attr("rel");

            if (idReport != "0") {
                $('#frameReport').html('<iframe id="ifrReport" width="99%" height="980px" src="Report.aspx?idReport=' + idReport + '">');
                $('#frameReport').show();

                e.preventDefault();
            }
        });
    });
</script>

“Report.aspx”

<%@ Page Language="C#" AutoEventWireup="true" Theme="theme" CodeFile="Report.aspx.cs"
    Inherits="Report" Title="Crystal Report" %>

<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
    Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
<head></head>
<body>
    <form id="ReportCrystal" runat="server">
    <div>
        <CR:CrystalReportViewer ID="crvReport" runat="server" AutoDataBind="true" />
    </div>
    </form>
</body>

“Report.aspx.cs”

private void ReportParameter()
{
    string reportName = "Test.rpt";
    string reportPath = Server.MapPath(String.Concat("~/reports/", reportName));
    string serverName = System.Configuration.ConfigurationManager.AppSettings["db"].ToString();
    string databaseName = String.Empty;
    string userId = Session["USERNAME"].ToString();
    string password = Session["PASSWORD"].ToString();

    if (File.Exists(reportPath))
    {
        ReportDocument reportDoc = new ReportDocument();
        reportDoc.Load(reportPath);

        foreach (ParameterField paramField in reportDoc.ParameterFields)
        {
            paramField.CurrentValues.Clear();
        }

        reportDoc.SetDatabaseLogon(userId, password, serverName, databaseName);

        ConnectionInfo crConnectionInfo = new ConnectionInfo();
        crConnectionInfo.Type = ConnectionInfoType.SQL;
        crConnectionInfo.AllowCustomConnection = true;
        crConnectionInfo.IntegratedSecurity = false;
        crConnectionInfo.ServerName = serverName;
        crConnectionInfo.DatabaseName = databaseName;
        crConnectionInfo.UserID = userId;
        crConnectionInfo.Password = password;

        this.ApplyConnection(reportDoc, crConnectionInfo);

        this.crvRelatorio.EnableParameterPrompt = true;
        this.crvRelatorio.ReportSource = reportDoc;
        this.crvRelatorio.RefreshReport();
    }
    else
    {
        this.divHeader.Visible = true;
        this.ltrTitulo.Text = String.Concat("Relatório não foi encontrado ");
    }
}

private void ApplyConnection(ReportDocument report, ConnectionInfo connectionInfo)
{
    this.ApplyLogOnInfo(report, connectionInfo);
    this.ApplyLogOnInfoForSubreports(report, connectionInfo);
}

private void ApplyLogOnInfo(ReportDocument reportDocument, ConnectionInfo connectionInfo)
{
    foreach (CrystalDecisions.CrystalReports.Engine.Table tableTemp in reportDocument.Database.Tables)
    {
        if (tableTemp.Name.ToUpper().StartsWith("COMMAND"))
        {

        }

        TableLogOnInfo tableLogonInfo = tableTemp.LogOnInfo;
        tableLogonInfo.ConnectionInfo = connectionInfo;
        tableTemp.ApplyLogOnInfo(tableLogonInfo);

        bool b = tableTemp.TestConnectivity();
    }
}

private void ApplyLogOnInfoForSubreports(ReportDocument reportDocument, ConnectionInfo connectionInfo)
{
    foreach (Section sectionTemp in reportDocument.ReportDefinition.Sections)
    {
        foreach (ReportObject reportObjectTemp in sectionTemp.ReportObjects)
        {
            if (reportObjectTemp.Kind == ReportObjectKind.SubreportObject)
            {
                SubreportObject subreportObject = (SubreportObject)reportObjectTemp;

                ReportDocument subReportDocument = subreportObject.OpenSubreport(subreportObject.SubreportName);

                this.ApplyLogOnInfo(subReportDocument, connectionInfo);
            }
        }
    }
}

private ParameterFields ApplyParameters(string paramName, object paramValue)
{
    ParameterFields paramFields = new ParameterFields();
    ParameterField paramField = new ParameterField();
    paramField.ParameterFieldName = paramName; // Crystal Report Parameter name.

    ParameterDiscreteValue pdvValue = new ParameterDiscreteValue();
    pdvValue.Value = paramValue;

    paramField.CurrentValues.Add(pdvValue);

    paramFields.Add(paramField);

    return paramFields;
}

的 “web.config”

<system.web>
    <compilation>
        <assemblies>
            <add assembly="CrystalDecisions.CrystalReports.Engine, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
            <add assembly="CrystalDecisions.ReportAppServer.ClientDoc, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
            <add assembly="CrystalDecisions.ReportSource, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
            <add assembly="CrystalDecisions.Shared, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
            <add assembly="CrystalDecisions.Web, Version=13.0.2000.0, Culture=neutral, PublicKeyToken=692FBEA5521E1304"/>
        </assemblies>
    </compilation>
</system.web>

答案 1 :(得分:0)

我认为您在更新面板中缺少报告源,这就是它可能无法正常工作的原因。另一方面,Crystal报表在更新面板中似乎不能很好地工作。我个人从来没有尝试过(我建立了一个报告门户但是使用了单独的页面)但是已经阅读了很多相关内容。即使人们在更新面板中显示它,通常工具栏上的打印和导出按钮也不起作用。 这个post提供了非常丰富的信息,并建议您可以采用几种不同的解决方案来获得可接受的解决方案。