views:

322

answers:

2

We're using the Telerik Rad Controls for ASP.Net Ajax on an ASP.Net MVC project. The RadChart generates the following 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="">

I'd like to remove the onerror attribute; I don't really want the customers being offered the option to see a stack trace if something goes wrong. I can't see any way to control the markup that this control generates. Google searches provide no help. Has anyone dealt with this before?

How do I remove the onerror attribute?

A: 

You could do something like this, just add it to the bottom of the page or call removeOnError in a load event somewhere.

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

Edit

Looking at Telerik's site it doesn't appear to be an option so the only way that I can think of is to override the Render event for your page and manually rip it out:

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();
            }
        }
    }
} 
Chris Haas
I'd really prefer not to send it down in the first place. If the image errors, it may fire the onerror event before the load event, meaning it will be too late to remove it.
Sean McMillan
Can you override render in an MVC app?
Sean McMillan
I don't have an MVC site running anywhere but I believe you override RenderView in the View Engine, http://msdn.microsoft.com/en-us/library/system.web.mvc.viewpage.renderview.aspx
Chris Haas
+1  A: 

Guys,

The onerror only ever shows in debug configuration. Once you deploy your app in Release the attribute does not get rendered!

Vladimir
Good deal. Is that documented anywhere, or do you just have to know it?
Sean McMillan