我有一个显示报告的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>
答案 0 :(得分:1)
如果要在会话中保存加载的Crystal Reports文档,则应在初始化 参数后执行此操作。目前发生奇怪的刷新事情已经太晚了,你在页面init中重新加载它的代码显然并不包含该参数加载代码。
public class GameFragment extends Fragment implements GameView {
private GamePresenter presenter;
@Override
public void onCreate(Bundle savedInstanceState){
presenter = new GamePresenter(this);
}
}
另一种选择是简单地将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
注意,我不确定ASP.NET中这些对象的资源管理方式如何;我知道在我的Windows应用程序中(将它们转换为PDF)我不得不非常小心地关闭文档以避免错误,因此第一种方法在这方面可能更可取,因为它只打开文档一次并保留对象
此外,嵌入式asp xml是否已经在页面加载时加载文档,再次没有这些必需的参数?