Printing MS Chart Control C# ASP.NET RRS feed

  • Question

  • User773040893 posted

    I found a pretty good post on how to create a Print Helper class to print controls but am having quite a bit of difficulty getting it to work with MS Chart controls. Just to make sure the code worked in general, I used it with a drop down list control and it worked just fine. I tried placing the chart control within a panel and used the panel as the control to print, to no avail. Here is the code for the PrintHelper class I found:

    public class PrintHelper
    	public PrintHelper()
        public static void PrintWebControl(Control ctrl)
            PrintWebControl(ctrl, string.Empty);
        public static void PrintWebControl(Control ctrl, string Script)
            StringWriter stringWrite = new StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
            if (ctrl is WebControl)
               Unit w = new Unit(691, UnitType.Pixel); ((WebControl)ctrl).Width = w;
            Page pg = new Page();
            pg.EnableEventValidation = false;
            if (Script != string.Empty)
            //if (Script == string.Empty)
                pg.ClientScript.RegisterStartupScript(pg.GetType(), "PrintJavaScript", Script);
            HtmlForm frm = new HtmlForm();
            frm.Attributes.Add("runat", "server");
            string strHTML = stringWrite.ToString();

    I called the method by passing the Chart control over to it and it blows up on the code where I assign the width. If I comment that out I get a null reference exception on pg.RenderControl(htmlWrite);

    It is almost as though this isn't being treated as a true WebControl. Does anyone have any ideas on how I can print the chart control?


    Friday, September 10, 2010 12:25 AM


All replies

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 10, 2010 3:07 AM
  • User-114806852 posted

    Have you tried setting the ImageStorageMode to UseImageLocation instead of UseHttpHandler, so that you can save the image of the chart to the filesystem instead of having it streamed? Maybe you could use that approach as an alternative.

    Friday, September 10, 2010 8:04 AM
  • User-1375663496 posted

    I have the same problem , and i have tried  to set the ImageStorageMode to UseImageLocation instead of UseHttpHandler , but it's not working.

    and when i try to print the chart only i find another mesage says "Chart width must be set in pixels."

    "System.ArgumentException was unhandled by user code
      Message="Chart width must be set in pixels."
           at System.Web.UI.DataVisualization.Charting.Chart.set_Width(Unit value)
           at PrintHelper.PrintWebControl(Control ctrl, String Script) in h:\AJAXShakawaWebSite1\App_Code\PrintHelper.cs:line 37
           at PrintHelper.PrintWebControl(Control ctrl) in h:\AJAXShakawaWebSite1\App_Code\PrintHelper.cs:line 28
           at Print.Page_Load(Object sender, EventArgs e) in h:\AJAXShakawaWebSite1\Print.aspx.cs:line 17
           at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
           at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
           at System.Web.UI.Control.OnLoad(EventArgs e)
           at System.Web.UI.Control.LoadRecursive()
           at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    Thursday, October 14, 2010 8:05 AM