MsChart:部分视图错误

发布于 2024-08-24 22:06:57 字数 1941 浏览 8 评论 0原文

当我在 MVC 项目上使用 Mschart 时遇到问题,当我使用项目的第一个索引页来渲染部分视图名称 index2 时,代码是

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

但是当我运行它时,出现错误,消息为

CS0029: Cannot 隐式将类型 'ASP.views_home_index2_ascx' 转换为 'System.Web.UI.Page'

- 它表示问题代码行是

: // 渲染图表控制

第 52 行:Chart2.Page = this; <<此处

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

第 54 行:Chart2.RenderControl(writer);

但是,当我将 Index2.ascx 中的所有代码放入 index.aspx 并且不渲染部分视图时,它工作正常

Index2.ascx 的代码是

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

%>

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

%>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

本王不退位尔等都是臣 2024-08-31 22:06:57

该错误似乎确实表明它需要位于 ASP.NET 页面内,可能是因为 MVC 架构发生了变化,并且视图并不真正使用页面/控件集合。

为了简化此过程,您可以创建一个 HTML 帮助器方法,以可重用的方式为您完成大部分工作。另外,您也可以尝试使用 a 并将控件嵌入到页面中作为替代方案。如果你想做辅助路线,你可以这样做:

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

HTH。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文