I seem to have to choose between Page_Init
, if I want my Crystal Report to not prompt for parameters on page change. Or Page_Load
if I want my dropdown list to work correctly in order to set a parameter. I want to do both though!
I assume this is because the dropdownlist doesn't exist during Page_Init
? How can I make this work? Relevant code is below:
ReportDocument rptDoc;
dsfeeEarnings ds1;
protected void Page_Init(object sender, EventArgs e)
{
var dt = new DataTable();
ds1 = new dsfeeEarnings();
dt.TableName = "Crystal Report Fee Earnings";
dt = getFeeEarnings1();
ds1.Tables[0].Merge(dt);
rptDoc = new ReportDocument();
rptDoc.Load(Server.MapPath("Report.rpt"));
rptDoc.SetDataSource(ds1);
CrystalReportViewer1.ReportSource = rptDoc;
}
dsfeeEarnings
is a .xsd
file that goes with the report.
getFeeEarnings1()
is the method that deals with the dropdown list's value and goes off to run a stored procedure using that value.
I have tried putting the code above into a if (!IsPostBack)
block but that seems to stop the report from working at all.
I tried creating a session instead, this helped with the dropdown list but hte report was still asking for the parameters each time I changed page.
I fixed this by using the following code:
ReportDocument rptDoc = new ReportDocument();
protected void Page_Init(object sender, EventArgs e)
{
CrystalReportViewer1.ReportSource = rptDoc;
}
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dsfeeEarnings ds = new dsfeeEarnings();
rptDoc.Load(Server.MapPath("FeeEarningsReport.rpt"));
rptDoc.SetDataSource(ds);
dt = getFeeEarnings1();
ds.Tables[0].Merge(dt);
}