0

我在 C# Webform 中遇到了 Crystal Report 的问题。我已经创建了一个网页,它能够通过 Crystal Report Viewer 成功加载报表,但问题是,一旦用户单击 Crystal Report Viewer 的打印按钮,它将导致页面变为 POSTBACk 从而呈现我的其他网页上的按钮无法再次触发已编程的事件。那么有没有人解决我的这个问题,请指导我?非常感谢您的帮助。先感谢您。下面是我的代码

using CrystalDecisions.CrystalReports.Engine
namespace Report1
{
   protected void Page_Load(..)
   {
      //Previous code
      //if (!Page.IsPostBack)
      //{  LoadReport();  }
      //else
      //{  LoadReport();  }

      //Recommended code
      LoadReport();
   }

   protected void Button1_Click()
   {
      Response.Redirect("MainPage.aspx");
   }

   protected void CrystalReportViewer1_Init()
   {}

   private void LoadReport()
   {
        ConnectionInfo connectionInfo = new ConnectionInfo();
        connectionInfo.ServerName = @"server";
        connectionInfo.DatabaseName = "tbl1";
        connectionInfo.UserID = "sa";
        connectionInfo.Password = "sa";
        connectionInfo.Type = ConnectionInfoType.SQL;
        connectionInfo.AllowCustomConnection = true;

        string strParam = Convert.ToString(Request.QueryString["Param"]);
        label1.Text = strParam;

        ReportDocument cryRpt = new ReportDocument();

        Report1 crystalReport1;
        crystalReport1 = new Report1();

        cryRpt.Load(Server.MapPath("CrystalReport1.rpt"));
        SetDBLogonForReport(connectionInfo, cryRpt);

        cryRpt.SetParameterValue("Param", strParam);
        CrystalReportViewer1.SelectionFormula = "{Param}";
        CrystalReportViewer1.ReportSource = crystalReport1;
        CrystalReportViewer1.ReportSource = cryRpt;
   }

    private void SetDBLogonForReport(ConnectionInfo connectionInfo, ReportDocument reportDocument)
    {
        Tables tables = reportDocument.Database.Tables;
        foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
        {
            TableLogOnInfo tableLogonInfo = table.LogOnInfo;
            tableLogonInfo.ConnectionInfo = connectionInfo;
            table.ApplyLogOnInfo(tableLogonInfo);
        }
    }
}

后面的代码

<%@Page Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Report.aspx.cs"       Inherits="Project.Report" %>

 <td class="style5">
    <asp:Button ID="MainButton" runat="server" onclick="MainButton_Click" 
       Text="Back To MainPage" Width="174px" CausesValidation="False" />
          </td>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
       <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" 
                EnableDatabaseLogonPrompt="False" ReuseParameterValuesOnRefresh="True"
                AutoDataBind="True" Height="1269px" ToolPanelWidth="200px" Width="881px" 
                oninit="CrystalReportViewer1_Init" GroupTreeImagesFolderUrl="" 
                GroupTreeStyle-ShowLines="False" ReportSourceID="CrystalReportSource1" 
                ToolbarImagesFolderUrl="" ToolPanelView="None" 
                EnableParameterPrompt="False" SeparatePages="False" HasCrystalLogo="False" 
                HasToggleGroupTreeButton="False" HasToggleParameterPanelButton="False" />
            <CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
                <Report FileName="CrystalReport1.rpt">
                </Report>
            </CR:CrystalReportSource>
</asp:Content>
4

2 回答 2

0

谢谢回复。所以我只需要在 Not Postback 期间调用 LoadReport 函数就可以了。我在该方法之前确实尝试过,但这会使我的报告打印按钮无法打印报告??

嗨 Campanolo_1,

使用 asp:button Response.Redirect() 仍然无法做到这一点。所以我决定使用 javascript: onclick 按钮来执行相同的重定向 [onclick=window.location('MainPage.aspx')].. 具有讽刺意味的是,它与 CrystalReportViewer 回发或 !postback 配合得很好。

于 2013-11-12T08:52:47.760 回答
0

你的问题就在这里:

protected void Page_Load(..)
{
  if (!Page.IsPostBack)
  {  LoadReport;  }
  else
  {  LoadReport;  }
}

无论是否回发,您都调用 LoadReport 函数。

于 2013-11-08T12:38:07.680 回答