CrystalReportViewer 忘记参数
CrystalReportViewer forgetting parameters
我有一个正在显示报告的 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 Whosebug!
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 Whosebug!
///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>
如果您想在会话中保存加载的 Crystal 报告文档,您应该在初始化它之后这样做,使用 参数。在奇怪的刷新发生的那一刻已经太晚了,你在页面初始化中重新加载它的代码特别是不包含参数加载代码。
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
中的所有内容拆分为一个新函数,并从按钮单击和初始化中调用它:
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)中我必须非常小心地关闭文档以避免出错,所以第一种方法可能更适合 aspect,因为它只打开文档一次并保留该对象。
此外,嵌入 asp xml 是否已经在页面加载时加载了文档,同样没有这些必需的参数?
我有一个正在显示报告的 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 Whosebug!
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 Whosebug!
///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>
如果您想在会话中保存加载的 Crystal 报告文档,您应该在初始化它之后这样做,使用 参数。在奇怪的刷新发生的那一刻已经太晚了,你在页面初始化中重新加载它的代码特别是不包含参数加载代码。
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
中的所有内容拆分为一个新函数,并从按钮单击和初始化中调用它:
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)中我必须非常小心地关闭文档以避免出错,所以第一种方法可能更适合 aspect,因为它只打开文档一次并保留该对象。
此外,嵌入 asp xml 是否已经在页面加载时加载了文档,同样没有这些必需的参数?