将数组从vb codebehind放入javascript数据表中

时间:2012-11-11 17:39:35

标签: asp.net arrays vb.net datatable typeconverter

所以我试图在我的asp.net网站https://google-developers.appspot.com/chart/interactive/docs/gallery/linechart

中使用这个javascript

我在codebehind中有一个数组,它包含我的数据,我将其转换为像这样的多维数据表。

<in codebehind vb>
Public Property datat As DataTable

For outerIndex As Integer = 0 To 2
    Dim newRow As DataRow = table.NewRow()
    For innerIndex As Integer = 0 To 2
            newRow(innerIndex) = Array(outerIndex, innerIndex)
    Next
    table.Rows.Add(newRow)
Next

datat = table

<in asp>

function drawChart() {
    var data = <%=datat%>

我总是得到一个未定义的错误数据表。

我的新作品

<in asp>

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(drawcharts);
    function drawcharts() {
        var data = google.visualization.arrayToDataTable([
            ['X', 'MW'],
      [<%=mwtimepublic%>, <%=mwarraypublic%>]
        ]);

        var options = {
            title: 'MW Trend'
        };

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
    });
</script>

        公共函数Dtable()作为DataTable         Dim startdate As String         Dim enddate As String

    If FirstRun = False Then
        datat.Clear()
    End If

    If FirstRun = True Then
        FirstRun = False
        enddate = Date.Today
        startdate = DateAdd(DateInterval.Day, -20, Date.Today)
    Else
        enddate = TextBox2.Text
        startdate = TextBox1.Text
    End If

'<confidential code></confidentialcode>

    Dim mwaray(pdata.Count)
    Dim mwtime(pdata.Count)
    Dim table As New DataTable
    table.Columns.Add("X")
    table.Columns.Add("MW")
    Dim i As Integer
    Dim x As Integer
    For i = 1 To pdata.Count
        mwaray(i) = pdata(i).Value
        If mwaray(i) > upperlimit Then
            upperlimit = mwaray(i) + 50
        End If
    Next

    For i = 1 To pdata.Count
        mwtime(i) = "'" & pdata(i).TimeStamp.LocalDate.Month.ToString() & "/" & pdata(i).TimeStamp.LocalDate.Day.ToString() & "/" & pdata(i).TimeStamp.LocalDate.Year.ToString() & " " & pdata(i).TimeStamp.LocalDate.TimeOfDay.Hours.ToString() & ":" & pdata(i).TimeStamp.LocalDate.TimeOfDay.Minutes.ToString() & ":" & pdata(i).TimeStamp.LocalDate.TimeOfDay.Seconds.ToString() & "'"
    Next

    For i = 1 To pdata.Count
        mwarraypublic.Add(mwaray(i))
    Next

    For i = 1 To pdata.Count
        mwtimepublic.Add(mwtime(i))
    Next

End Function

我还没有得到绘制图表

2 个答案:

答案 0 :(得分:0)

我制作了一个关于如何在从代码隐藏中获取javascript的数组时创建jquery ui自动完成的视频

跳到6:00看看我是如何制作阵列的 http://www.youtube.com/watch?v=ULHSqoDHP-U&list=UUgtKyNvllU9E6c2Mi8OtzCQ&index=6&feature=plcp

PageMethods.YourWebMethod(function(results){
  var data = resluts;
  //the rest of your code goes here...
});

更新


Google实际上将数组数组转换为数据表。 换句话说,您必须返回一个行数组,而它们自己的行是一个列值数组。

所以在您的代码背后,您执行以下操作 首先导入webservices

Imports System.Web.Services

然后使用返回arraylist的函数创建webmethod

<WebMethod()>
Public Shared Function Dtable() As ArrayList
dim table As New ArrayList
Dim YourDataSetTable as new Dataset.YorDataTable
'fill you table here

'first get column names as the first row/array
dim colNames as New ArrayList

For Each col as DataColumn in YourDataSetTable.Columns
   colNames.Add(col.ColumnName)
Next
table.Add(colNames)

'then on to the data
For Each r as DataRow in YourDataSetTable .Rows
  dim colVals as New ArrayList
  For Each col as DataColumn in YourDataSetTable.Columns
    colVals.Add(r.Item(col.ColumnName))
  Next
  table.Add(colVals)
Next
Return table
End Function

然后在javascript中

PageMethods.Dtable(function(results){
  var data = resluts;
  //the rest of your code goes here...
});

确保页面上有一个脚本管理器,并启用了pagemethods。

答案 1 :(得分:0)

您发布的代码示例不应编译。在Visual Basic中,auto-implemented properties就是你在代码第一行看到的内容,后跟一个与数据表无关的块。

尝试将代码放入属性getter:

Public ReadOnly Property datat As DataTable
   Get
      For outerIndex As Integer = 0 To 2
          Dim newRow As DataRow = table.NewRow()

          For innerIndex As Integer = 0 To 2
              newRow(innerIndex) = Array(outerIndex, innerIndex)
          Next

          table.Rows.Add(newRow)
      Next

      Return table
   End Get
End Property