I have an aspx page that loads an RDLC report.
When I load the report on a post back (i.e. by adding a button to the page that does nothing but post the page back and call the DoReport() method), the report loads fine. However, if I try to load the report directly on page load (i.e. I put the DoReport() method in Page_Load, the page seems to run endlessly, and Page_Load is called hundreds of times.
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.QueryString["ReportID"]))
{
BuildReport(Convert.ToInt32(Request.QueryString["ReportID"]));
}
}
private void BuildReport(int reportID_)
{
Database db = DatabaseFactory.CreateDatabase();
DataTable tbl = db.ExecuteDataSet(CommandType.Text, "select top 10 * from TABLE_NAME").Tables[0];
RdlcBuilder rdlcBuilder = new RdlcBuilder(reportID_); //custom class that builds the RDLC based on the report ID
XmlDocument xmlDoc = new XmlDocument();
Bind(rdlcBuilder.GetRdlcStream(xmlDoc), tbl);
}
private void Bind(Stream reportDefinitionStream_, DataTable dataSource_)
{
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.Width = new Unit(700);
ReportViewer1.Height = new Unit(1200);
ReportViewer1.LocalReport.EnableExternalImages = true;
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetName", dataSource_));
ReportViewer1.LocalReport.LoadReportDefinition(reportDefinitionStream_);
}
Again – this page works fine if I call BuildReport(int reportID_) from a command button
Assigning the Report Definition to the Report triggers another Page_Load, when the ReportViewer asynchronously populates itself (while the ‘loading’ symbol is displayed).
Since you are assigning the report definition within every Page_Load, your code is indirectly invoking Page_Load recursively.
A simple fix is to perform a check for IsPostBack: