views:

242

answers:

1

Hi

I have a problem when i using Mschart on my MVC project, when i use the first index page of project to render for the partial view name index2 the code is

<% Html.RenderPartial("Index2"); %>

But when i run it the error is occur which the message is

CS0029: Cannot implicitly convert type 'ASP.views_home_index2_ascx' to 'System.Web.UI.Page'

-it said that the problem line of code is

: // Render chart control

Line 52: Chart2.Page = this; << At here

Line 53: HtmlTextWriter writer = new HtmlTextWriter(Page.Response.Output);

Line 54: Chart2.RenderControl(writer);

But when i put all of code in Index2.ascx to the index.aspx and not to render the partial view it work fine

Code of Index2.ascx is

 <%   
            System.Web.UI.DataVisualization.Charting.Chart Chart2 = new System.Web.UI.DataVisualization.Charting.Chart();
            Chart2.Width = 412;
            Chart2.Height = 296;
            Chart2.RenderType = RenderType.ImageTag;

            Chart2.Palette = ChartColorPalette.BrightPastel;
            Title t = new Title("No Code Behind Page", Docking.Top, new System.Drawing.Font("Trebuchet MS", 14, System.Drawing.FontStyle.Bold), System.Drawing.Color.FromArgb(26, 59, 105));
            Chart2.Titles.Add(t);
            Chart2.ChartAreas.Add("Series 1");

            Chart2.Series.Add("Series 1");

            // add points to series 1
            Chart2.Series["Series 1"].Points.AddY(3);
            Chart2.Series["Series 1"].Points.AddY(4);
            Chart2.Series["Series 1"].Points.AddY(5);

            Chart2.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
            Chart2.BorderColor = System.Drawing.Color.FromArgb(26, 59, 105);
            Chart2.BorderlineDashStyle = ChartDashStyle.Solid;
            Chart2.BorderWidth = 2;

            Chart2.Legends.Add("Legend1");

            // Render chart control
            Chart2.Page = this;
            HtmlTextWriter writer = new HtmlTextWriter(Page.Response.Output);
            Chart2.RenderControl(writer);

%>

A: 

Hey,

The error does seem to suggest that it is required to be within an ASP.NET page, probably because with the changes to the MVC architecture and how the views don't really use the page/control collection.

To simplify this process, you could create an HTML helper method that does most of that work for you in a reusable way. Also, you could try using a and just embed the control within the page too as an alternative. If you want to do the helper route, you can do:

public static class ChartExtensions
{
   public static string Chart(this HtmlHelper html, <settings>)
   {
     //Put code here, return a string
   }
}

HTH.

Brian