簡體   English   中英

在page_load上加載RDLC

[英]Load an RDLC on page_load

我有一個加載RDLC報告的aspx頁面。

當我在回發頁面上加載報告時(即通過在頁面上添加一個僅執行回發頁面並調用DoReport()方法的按鈕),報告可以正常加載。 但是,如果我嘗試直接在頁面加載時加載報告(即,我將DoReport()方法放在Page_Load中,則頁面似乎會無休止地運行,並且Page_Load被調用了數百次。

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_);

}

再次-如果我從命令按鈕調用BuildReport(int reportID_),則此頁面工作正常

當ReportViewer異步填充自身時(在顯示“正在加載”符號時),將報告定義分配給報告會觸發另一個Page_Load。

由於您要在每個Page_Load中分配報告定義,因此您的代碼將間接遞歸調用Page_Load。

一個簡單的修復方法是對IsPostBack進行檢查:

protected void Page_Load(object sender, EventArgs e)
{
    if(IsPostBack)
        return;
    //...
    // Assign report definition.
    //...

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM