代码之家  ›  专栏  ›  技术社区  ›  Sean McMillan

如何防止telerik radchart生成onerror属性?

  •  1
  • Sean McMillan  · 技术社区  · 16 年前

    我们在一个asp.net mvc项目中使用了用于asp.net ajax的telerik rad控件。radchart生成以下html:

    <img onerror="if(confirm('Error loading RadChart image.\nYou may also wish to check the ASP.NET Trace for further details.\nDisplay stack trace?'))window.location.href=this.src;" src="ChartImage.axd?UseSession=true&amp;ChartID=e25ad666-e05b-4a92-ac0c-4f2c729b9382_chart_ctl00$MainContent$AverageCTMChart&amp;imageFormat=Png&amp;random=0.501658702968461" usemap="#imctl00_MainContent_AverageCTMChart" style="border-width: 0px;" alt="">
    

    我想把 onerror 属性;我真的不想让客户在出现问题时看到堆栈跟踪。我看不到任何方法来控制此控件生成的标记。谷歌搜索没有提供帮助。以前有人处理过吗?

    如何删除 出错 属性?

    3 回复  |  直到 12 年前
        1
  •  1
  •   Vladimir    16 年前

    onerror只在调试配置中显示。一旦在release中部署应用程序,属性就不会被呈现!

        2
  •  0
  •   Chris Haas    16 年前

    您可以这样做,只需将其添加到页面底部,或者在某个加载事件中调用removeornerror。

    function removeOnError(){
        //Grab all images
        var imgs = document.getElementsByTagName('img');
        for(var i=0;i<imgs.length;i++){
            //If they've got the onerror attribute
            if(imgs[i].onerror){
                //set it to null
                imgs[i].onerror = null;
            }
        }
    }
    //Call the function above
    removeOnError();
    

    编辑

    查看Telerik的站点,它似乎不是一个选项,因此我唯一能想到的方法是覆盖页面的呈现事件并手动将其删除:

    protected override void Render(HtmlTextWriter writer)
    {
        using (System.IO.MemoryStream MS = new System.IO.MemoryStream())
        {
            using (System.IO.StreamWriter SW = new System.IO.StreamWriter(MS))
            {
                HtmlTextWriter NW = new HtmlTextWriter(SW);
                base.Render(NW);
                NW.Flush();
                MS.Position = 0;
                using (System.IO.StreamReader SR = new System.IO.StreamReader(MS))
                {
                    string html = SR.ReadToEnd();
                    MatchCollection MC = Regex.Matches(html, "<img.*?(?<OnError>onerror=\".*?\").*?>");
                    foreach (Match M in MC)
                    {
                        if (M.Success)
                        {
                            html = html.Replace(M.Groups["OnError"].Value, "");
                        }
                    }
                    Response.Write(html);
                    SR.Close();
                }
            }
        }
    } 
    
        3
  •  0
  •   RR-Fireball    12 年前

    Telerik控件检查属性

    HttpContext.Current.IsDebuggingEnabled
    

    决定是否生成onerror属性。因此要删除这些块,请确保在web.config的“compilation”节点中关闭调试

    <compilation debug="false">