我有一个显示报告的CrystalReportViewer。我将两个参数传递给它,这将改变数据。这是可行的,除非您尝试进一步深入到报表中,此时它会重置为默认参数。
这有点像reportviewer元素刷新并忘记了所有东西。我确实有一个page_init方法,它试图将值推入,但是我得到了对象引用错误,所以可能做错了。
我还应该试试什么?请参阅下面的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["ReportSource"] != null)
{
CrystalReportViewer1.ReportSource = (ReportDocument)Session["ReportSource"];
}
else
{
ReportDocument reportDocument = new ReportDocument();
reportDocument.Load(Server.MapPath("~/Report/In Out.rpt"));
reportDocument.SetDatabaseLogon("user", "pass" ,@"server", "db"); //removed credentials for stackoverflow!
CrystalReportViewer1.ReportSource = reportDocument;
Session["ReportSource"] = reportDocument;
}
}
protected void butReport_Click(object sender, EventArgs e)
{
ReportDocument reportDocument = new ReportDocument();
reportDocument.Load(Server.MapPath("./Report/In Out.rpt"));
reportDocument.SetDatabaseLogon("user", "pass" ,@"server", "db"); //removed credentials for stackoverflow!
///Load Session variables with values from web-controls
Session["@FromDate"] = ConvertToDateTime(Request.Form["StartDate"]);
Session["@ToDate"] = ConvertToDateTime(Request.Form["EndDate"]);
reportDocument.SetParameterValue("Start", Session["@FromDate"]);
reportDocument.SetParameterValue("End", Session["@ToDate"]);
CrystalReportViewer1.ReportSource = reportDocument;
}我的CR嵌入如下
<div align="center">
<asp:TextBox ID="StartDate" runat="server" type="date" placeholder="e.g. 31/12/2014" ></asp:TextBox>
<asp:TextBox ID="EndDate" runat="server" type="date" placeholder="e.g. 31/12/2014" ></asp:TextBox>
<asp:button runat="server" id="Submit" type="button" value="Show Report" Text="View Report" onclick="butReport_Click" /></div>
<div>
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
AutoDataBind="True" EnableDatabaseLogonPrompt="False"
GroupTreeImagesFolderUrl="" Height="940px"
ToolbarImagesFolderUrl="" ToolPanelWidth="200px" Width="1411px"
EnableParameterPrompt="False" ReuseParameterValuesOnRefresh="True"
ReportSourceID="CrystalReportSource1" />
<CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
<Report FileName="Report\In Out.rpt">
</Report>
</CR:CrystalReportSource>
</div>发布于 2016-04-22 12:53:03
如果要在会话中保存已加载的水晶报表文档,则应在使用参数初始化该文档之后进行保存。目前,这种奇怪的刷新已经太晚了,您在页面init中重新加载它的代码显然不包含该参数加载代码。
protected void Page_Load(object sender, EventArgs e)
{
if (Session["ReportSource"] != null)
CrystalReportViewer1.ReportSource = (ReportDocument)Session["ReportSource"];
}
protected void butReport_Click(object sender, EventArgs e)
{
ReportDocument reportDocument = new ReportDocument();
/* your code to load the document and assign it as datasource to the CrystalReportViewer */
// Assign your result to the session:
Session["ReportSource"] = reportDocument;
}另一个选项是简单地将butReport_Click中的所有内容拆分到一个新函数中,并通过单击按钮和init调用它:
protected void Page_Load(object sender, EventArgs e)
{
PrepareCR();
}
protected void butReport_Click(object sender, EventArgs e)
{
PrepareCR();
}
protected void PrepareCR()
{
ReportDocument reportDocument = new ReportDocument();
/* your code to load the document and assign it as datasource to the CrystalReportViewer */
}注意,我不确定ASP.NET中这些对象的资源管理是如何进行的;我知道在我的Windows (将它们转换为PDF)中,我必须非常小心地关闭文档,以避免出现错误,因此在这方面,第一种方法可能更好,因为它只打开文档一次并保留该对象。
另外,嵌入asp xml是否已经在页面加载时加载了文档,同样没有这些必需的参数?
https://stackoverflow.com/questions/36791902
复制相似问题