这个DevExpress的ReportViewer折磨死我了。
把它和RadGrid等控件放在同一个页面上,用RadAjaxManager控制它们进行异步刷新。结果发现异步刷新了某个控件之后,那个DevExpress的ReportViewer的样式就全都显示不出来了(字体、颜色甚至表格线都看不到了)。查看页面源文件,发现这个ReportViewer会把自己输出成一个Div,然后再在后面追加输出一堆Style:
Style
<style type="text/css">
.cs6591E277 {}{color:#000000;background-color:#FFFFFF;border-left:#000000 1px solid;border-top:#000000 1px solid;border-right:#000000 1px solid;border-bottom:#000000 1px solid;font-family:楷体_gb2312; font-size:9pt; font-weight:bold; font-style:normal; padding-left:2px;padding-right:2px;}
.csD3080846 {}{color:#000000;background-color:#FFFFFF;border-left:#000000 1px solid;border-top-style: none;border-right:#000000 1px solid;border-bottom:#000000 1px solid;font-family:楷体_gb2312; font-size:9pt; font-weight:normal; font-style:normal; padding-left:2px;padding-right:2px;}
.cs73023F8C {}{color:#000000;background-color:#FFFFFF;border-left-style: none;border-top:#000000 1px solid;border-right:#000000 1px solid;border-bottom:#000000 1px solid;font-family:楷体_gb2312; font-size:9pt; font-weight:bold; font-style:normal; padding-left:2px;padding-right:2px;}
.csBFB73F4C {}{color:#000000;background-color:#FFFFFF;border-left-style: none;border-top-style: none;border-right:#000000 1px solid;border-bottom:#000000 1px solid;font-family:楷体_gb2312; font-size:9pt; font-weight:normal; font-style:normal; padding-left:2px;padding-right:2px;}
.csD7D3D909 {}{color:#000000;background-color:transparent;border-left:#000000 1px solid;border-top-style: none;border-right-style: none;border-bottom:#000000 1px solid;font-family:楷体_gb2312; font-size:9pt; font-weight:normal; font-style:normal; }
.cs9195D68E {}{color:#000000;background-color:transparent;border-left:#000000 1px solid;border-top-style: none;border-right-style: none;border-bottom-style: none;font-family:楷体_gb2312; font-size:9pt; font-weight:normal; font-style:normal; }
.cs119DE757 {}{color:#000000;background-color:transparent;border-left-style: none;border-top-style: none;border-right:#000000 1px solid;border-bottom:#000000 1px solid;font-family:楷体_gb2312; font-size:9pt; font-weight:normal; font-style:normal; }
.cs29BC36A1 {}{color:#000000;background-color:transparent;border-left-style: none;border-top-style: none;border-right:#000000 1px solid;border-bottom-style: none;font-family:楷体_gb2312; font-size:9pt; font-weight:normal; font-style:normal; }
.cs7FE4C565 {}{color:#000000;background-color:transparent;border-left-style: none;border-top-style: none;border-right-style: none;border-bottom:#000000 1px solid;font-family:楷体_gb2312; font-size:9pt; font-weight:normal; font-style:normal; }
.csB44DACD1 {}{color:#000000;background-color:transparent;border-left-style: none;border-top-style: none;border-right-style: none;border-bottom-style: none;font-family:楷体_gb2312; font-size:9pt; font-weight:normal; font-style:normal; }
.cs8ADCFAE6 {}{color:#008080;background-color:#FFFFFF;border-left-style: none;border-top-style: none;border-right-style: none;border-bottom-style: none;font-family:楷体_gb2312; font-size:24pt; font-weight:bold; font-style:normal; padding-left:2px;padding-right:2px;}
.csF7D3565D {}{height:0px;width:0px;overflow:hidden;font-size:0px;line-height:0px;}
</style>
<style type="text/css">
.cs6591E277 {}{color:#000000;background-color:#FFFFFF;border-left:#000000 1px solid;border-top:#000000 1px solid;border-right:#000000 1px solid;border-bottom:#000000 1px solid;font-family:楷体_gb2312; font-size:9pt; font-weight:bold; font-style:normal; padding-left:2px;padding-right:2px;}
.csD3080846 {}{color:#000000;background-color:#FFFFFF;border-left:#000000 1px solid;border-top-style: none;border-right:#000000 1px solid;border-bottom:#000000 1px solid;font-family:楷体_gb2312; font-size:9pt; font-weight:normal; font-style:normal; padding-left:2px;padding-right:2px;}
.cs73023F8C {}{color:#000000;background-color:#FFFFFF;border-left-style: none;border-top:#000000 1px solid;border-right:#000000 1px solid;border-bottom:#000000 1px solid;font-family:楷体_gb2312; font-size:9pt; font-weight:bold; font-style:normal; padding-left:2px;padding-right:2px;}
.csBFB73F4C {}{color:#000000;background-color:#FFFFFF;border-left-style: none;border-top-style: none;border-right:#000000 1px solid;border-bottom:#000000 1px solid;font-family:楷体_gb2312; font-size:9pt; font-weight:normal; font-style:normal; padding-left:2px;padding-right:2px;}
.csD7D3D909 {}{color:#000000;background-color:transparent;border-left:#000000 1px solid;border-top-style: none;border-right-style: none;border-bottom:#000000 1px solid;font-family:楷体_gb2312; font-size:9pt; font-weight:normal; font-style:normal; }
.cs9195D68E {}{color:#000000;background-color:transparent;border-left:#000000 1px solid;border-top-style: none;border-right-style: none;border-bottom-style: none;font-family:楷体_gb2312; font-size:9pt; font-weight:normal; font-style:normal; }
.cs119DE757 {}{color:#000000;background-color:transparent;border-left-style: none;border-top-style: none;border-right:#000000 1px solid;border-bottom:#000000 1px solid;font-family:楷体_gb2312; font-size:9pt; font-weight:normal; font-style:normal; }
.cs29BC36A1 {}{color:#000000;background-color:transparent;border-left-style: none;border-top-style: none;border-right:#000000 1px solid;border-bottom-style: none;font-family:楷体_gb2312; font-size:9pt; font-weight:normal; font-style:normal; }
.cs7FE4C565 {}{color:#000000;background-color:transparent;border-left-style: none;border-top-style: none;border-right-style: none;border-bottom:#000000 1px solid;font-family:楷体_gb2312; font-size:9pt; font-weight:normal; font-style:normal; }
.csB44DACD1 {}{color:#000000;background-color:transparent;border-left-style: none;border-top-style: none;border-right-style: none;border-bottom-style: none;font-family:楷体_gb2312; font-size:9pt; font-weight:normal; font-style:normal; }
.cs8ADCFAE6 {}{color:#008080;background-color:#FFFFFF;border-left-style: none;border-top-style: none;border-right-style: none;border-bottom-style: none;font-family:楷体_gb2312; font-size:24pt; font-weight:bold; font-style:normal; padding-left:2px;padding-right:2px;}
.csF7D3565D {}{height:0px;width:0px;overflow:hidden;font-size:0px;line-height:0px;}
</style>
估计是异步刷新之后这些Style没被认出来。解决方法是在Page_Load里调用Report的ExportToHtml()方法获取它的全部HTML,然后使用正则表达式截取Style那部分,然后使用Page.ClientScript.RegisterClientScriptBlock()把这些Style注册先。
RegisterReportStyle
private void RegisterReportStyle(int ReportCode)
{
string key = string.Format("Report{0}Style", ReportCode);
if (ViewState[key] == null || string.IsNullOrEmpty(ViewState[key].ToString()))
{
// 创建报表对象
IReportTemplateHistoryManager rptHisMgr = ContainerHelper.CreateInstanceByContainer<IReportTemplateHistoryManager>();
ReportTemplateHistoryEntity rptHisEntity = rptHisMgr.GetCurrentReportTemplateHistory(ReportCode);
MemoryStream ms = new MemoryStream(rptHisEntity.ReportContent);
XtraReport rpt = XtraReport.FromStream(ms, true);
//获取报表Style
MemoryStream mStream = new MemoryStream();
rpt.ExportToHtml(mStream);
StreamReader sReader = new StreamReader(mStream);
mStream.Position = 0;
string htmlText = sReader.ReadToEnd();
Regex regex = new Regex("<style type=\"text/css\">.*</style>", RegexOptions.Singleline);
Match matchStyleHtml = regex.Match(htmlText);
ViewState[key] = matchStyleHtml.Value;
}
//向页面输出报表Style
if (Page.ClientScript.IsClientScriptBlockRegistered(typeof(Page), key) == false)
{
string sss = ViewState[key].ToString();
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), key, ViewState[key].ToString());
}
}
private void RegisterReportStyle(int ReportCode)
{
string key = string.Format("Report{0}Style", ReportCode);
if (ViewState[key] == null || string.IsNullOrEmpty(ViewState[key].ToString()))
{
// 创建报表对象
IReportTemplateHistoryManager rptHisMgr = ContainerHelper.CreateInstanceByContainer<IReportTemplateHistoryManager>();
ReportTemplateHistoryEntity rptHisEntity = rptHisMgr.GetCurrentReportTemplateHistory(ReportCode);
MemoryStream ms = new MemoryStream(rptHisEntity.ReportContent);
XtraReport rpt = XtraReport.FromStream(ms, true);
//获取报表Style
MemoryStream mStream = new MemoryStream();
rpt.ExportToHtml(mStream);
StreamReader sReader = new StreamReader(mStream);
mStream.Position = 0;
string htmlText = sReader.ReadToEnd();
Regex regex = new Regex("<style type=\"text/css\">.*</style>", RegexOptions.Singleline);
Match matchStyleHtml = regex.Match(htmlText);
ViewState[key] = matchStyleHtml.Value;
}
//向页面输出报表Style
if (Page.ClientScript.IsClientScriptBlockRegistered(typeof(Page), key) == false)
{
string sss = ViewState[key].ToString();
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), key, ViewState[key].ToString());
}
}