如何在服务器代码运行MVC2之前运行javascript

时间:2013-05-09 01:42:38

标签: javascript asp.net-mvc-2 .net-4.0 asp.net-charts

好的,事情就是这样,

我有一个视图,它呈现图表(.net图表),我希望图表与Web浏览器窗口的大小相同(浏览器将始终是IE 9)。

我尝试了几个例子,但没有一个适合我。由于图表被渲染为图像,我需要在图表呈现之前知道浏览器的大小(这发生在服务器端)。

我的意图是人们只需通过URL发送参数就可以创建图表,这样就可以从另一个View或控制器中获取这些值。

有办法做到这一点吗?这是查看代码

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Charts.Master" Inherits="System.Web.Mvc.ViewPage<KpiDataBase.Models.ChartModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<%if (Model.Series != null)
{         
System.Web.UI.DataVisualization.Charting.Chart Chart1 = new System.Web.UI.DataVisualization.Charting.Chart();

Chart1.Width = 1400; -> this are the values that need to be dynamic 
Chart1.Height = 1100;-> this are the values that need to be dynamic 

 ... code for generating the chart....

// Render chart control
Chart1.Page = this;
HtmlTextWriter writer2 = new HtmlTextWriter(Page.Response.Output);
Chart1.RenderControl(writer2);                                 
}%>  
</asp:Content>

非常感谢任何帮助。通过在互联网上搜索,我发现这只能通过JavaScript实现。如果可以运行此脚本:

    <script type="text/javascript">

    function getWidth() {
        var width = $(window).width();}
    return width, height ;
 </script>

 <script type="text/javascript">

    function getHeigth() {
        var height = $(window).height();}
    return height ;
  </script>

然后使用这些值来渲染我的图表,这会很棒。

1 个答案:

答案 0 :(得分:0)

最后,我使用Ajax来执行此操作。

<script type="text/javascript">
$(document).ready(function () {
    var width = screen.width;
    var height = screen.height;
    $.ajax({
        type: "GET",
        url: "/Charts/Chart",
        data: { 'width': width, 'height': height },
        success: function () {
        }
    });
}); 

相关问题