後來只好自己debug找答案了。
後來我發現,在點擊“打印”按鈕時,會使頁面發生postback,從而使report重新加載,我個人認爲就在問題就在這裏,CrystalReportViewer在點擊打印時,頁面會自動進行回傳,然後再服務器端重新加載Report的內容,同時彈出打印的對話框,但是在這個過程中,,CrystalReportViewer會重新加載,也就是會重新連接數據庫。所以你要在postBack過程中再給你的CrystalReportViewer設置一下數據庫的連接字符串。
所以通常解決這個的辦法是在page_load事件中綁定CrystalReportViewer的數據源,當然你也可以在CrystalReportViewer的initial事件中進行綁定,但是由於這是CrystalReportViewer還沒有加載,此時你不能顯示的使用CrystalReportViewer進行綁定。總結一下這兩種方法:
1.在page_load事件中綁定:
(a) CrystalReportViewer的autoDataBind屬性設爲false
cryptSource.ReportDocument.SetDatabaseLogon("sa", "sa", "demoServer", "test");
cryptSource.ReportDocument.VerifyDatabase();
cryptSource.DataBind();
CrystalReportViewer1.DataBind();
cryptSource.ReportDocument.SetDatabaseLogon("sa", "sa", "demoServer", "test");
cryptSource.ReportDocument.VerifyDatabase();
cryptSource.DataBind();
(a) CrystalReportViewer的autoDataBind屬性設爲false
這種情況在上面已經提過,由於CrystalReportViewer控件還沒有加載,所以不能顯示綁定。
(b) CrystalReportViewer的autoDataBind屬性設爲true
cryptSource.ReportDocument.SetDatabaseLogon("sa", "sa", "demoServer", "test");
cryptSource.ReportDocument.VerifyDatabase();
cryptSource.DataBind();