我在 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>