在 Javascript 中,我调用了一个从数据表中检索数据并将其转换为 javascriptSerializer 的函数,然后我在 Javascript 图表中使用数据。第一次一切正常,一旦我更改数据集选择参数,即日期等,并再次调用 Function PopulateChart() 以尝试提取一组新数据,我在 javascript 中检索它时遇到问题。数据和 json 字符串在服务器上正确填充,但在客户端上不正确,旧的数据字符串仍然存在。
var chartData 仍然显示旧数据,即使通过返回的序列化程序。Serialize(rows) 显示服务器上的正确数据
下面是我的代码以及我想要实现的目标。
ASPX
var chartData = <%= PopulateChart() %>;
VB
Public Function PopulateChart() As String
''Get Chart Data
Dim daChart As New dsSVTableAdapters.clsChart
Dim dtChart As New dsSV.webV1_ChartsDataTable
Dim drChart As dsSV.webV1_ChartsRow
dtChart = daChart.GetChart(hChartID.Value)
If dtChart.Rows.Count > 0 Then
drChart = dtChart.Rows(0)
hChartName.Value = drChart.Description
HInterval.Value = drChart.Interval
Dim dtData As DataTable
Dim ds As New DataSet()
ds = GetData(4, DateTime.Parse("2012-05-01"), DateTime.Parse("2012-06-30"))
dtData = ds.Tables(0)
Dim serializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
Dim rows As New List(Of Dictionary(Of String, Object))
Dim row As Dictionary(Of String, Object)
For Each dr As DataRow In dtData.Rows
row = New Dictionary(Of String, Object)
For Each col As DataColumn In dtData.Columns
If col.ColumnName = "DateAndTime" Then
Dim dts As DateTime = DateTime.Parse(dr(col).ToString())
row.Add(col.ColumnName, dts.ToString("yyyy-MM-dd hh:mm:ss"))
ElseIf col.ColumnName = "Time" Then
Dim ts As DateTime = DateTime.Parse(dr(col).ToString())
row.Add(col.ColumnName, ts.ToString("hh:mm:ss"))
Else
row.Add(col.ColumnName, dr(col))
End If
Next
rows.Add(row)
Next
serializer.MaxJsonLength = Int32.MaxValue
Return serializer.Serialize(rows)
End If
End Function