我在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>
答案 0 :(得分:0)
您的问题就在这里:
protected void Page_Load(..)
{
if (!Page.IsPostBack)
{ LoadReport; }
else
{ LoadReport; }
}
您调用LoadReport函数是否回发。
答案 1 :(得分:0)
感谢您的回复。所以我只需要在Not Postback期间调用LoadReport函数。我在该方法之前尝试过,但这会使我的报告打印按钮无法打印报告而不是?
嗨Campanolo_1,
使用asp:按钮Response.Redirect()仍然无法做到这一点。所以我决定使用javascript:onclick按钮进行相同的重定向[onclick = window.location('MainPage.aspx')] ..具有讽刺意味的是,它对CrystalReportViewer回发或!回发非常有用。