Javascript 从代码隐藏向google图表传递数据

Javascript 从代码隐藏向google图表传递数据,javascript,c#,asp.net,Javascript,C#,Asp.net,我是一个新手,我在ASP.net上开发了一个项目,该项目需要Google Charts API来生成一些图表,当我在Google Charts中使用静态数据时,一切都很顺利,问题是我想传递从代码隐藏页(.cs页)获取的数据,我该怎么做 //这是我的脚本代码 var res; function GetCityValue() { PageMethods.GetCity("USA", onSucceeded, onFailed); } function on

我是一个新手,我在ASP.net上开发了一个项目,该项目需要Google Charts API来生成一些图表,当我在Google Charts中使用静态数据时,一切都很顺利,问题是我想传递从代码隐藏页(.cs页)获取的数据,我该怎么做

//这是我的脚本代码

var res;
   function GetCityValue() {
       PageMethods.GetCity("USA", onSucceeded, onFailed);
     }
       function onSucceeded(result)
       {

         res=result; 
       }
       function onFailed(error)
       {
           return 0;
       }




   // Google chart API

       // Load the Visualization API and the piechart package.
       google.load('visualization', '1.0', { 'packages': ['corechart'] });

       // Set a callback to run when the Google Visualization API is loaded.
       google.setOnLoadCallback(drawChart);


       // Callback that creates and populates a data table, 
       // instantiates the pie chart, passes in the data and
       // draws it.
       function drawChart() {

           // Create the data table.
           var data = new google.visualization.DataTable();
           data.addColumn('string', 'Topping');
           data.addColumn('number', 'Slices');

           GetCityValue();

           data.addRows([
    ['Mushrooms', res],
    ['Onions', 1],
    ['Olives', 1],
    ['Zucchini', 1],
    ['Pepperoni', 2]
  ]);

           // Set chart options
           var options = { 'title': 'How Much Pizza I Ate Last Night',
               'width': 400,
               'height': 300
           };

           // Instantiate and draw our chart, passing in some options.
           var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
           chart.draw(data, options);
       }
我的HTML代码

<form id="form1" runat="server">
   <div>

   </div>
   <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
  </asp:ScriptManager>
  <div id="chart_div" style="width:400; height:300"></div> 
  </form>

}

要将数据从代码隐藏传递到google图表,我们必须更改代码,您应该创建一个web方法

如何更改代码以及如何创建web方法。。。只需检查以下链接它的工作为我好

  namespace pageMethodsAndWebMethods
 {
   public partial class jscript : System.Web.UI.Page
   {
       protected void Page_Load(object sender, EventArgs e)
       {

       }
    [System.Web.Services.WebMethod()]
    public static int GetCity(String strCountry)
    {
        int strCity = 0;
        if (strCountry == "USA")
            strCity = 2;
        else
            strCity = 1;
        return strCity;
    }

}