动态添加DevExpress ReportViewer 的 Style

在DevExpress的ReportViewer与RadGrid结合使用时,通过RadAjaxManager进行异步刷新会导致ReportViewer样式丢失。本文介绍了一种解决方案,即在Page_Load中通过ExportToHtml方法获取并保留样式。

这个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没被认出来。解决方法是在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());
    }

}

转载于:https://www.cnblogs.com/1-2-3/articles/1237269.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值