从 C# 向 Javascript 发送参数
send parameter to Javascript from c#
我在 javascript 上遇到了一些问题。我给js发送参数发送数据是:
即将到来的javascript是:
c#代码:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' JSonID.Value = "{""id"":""1"",""name"":""1"",""data"":""0,3048"",""children"":[{""id"":""11"",""name"":""11"",""data"":""0,3048"",""children"":[{""id"":""111"",""name"":""111"",""data"":""0,3048"",""children"":[]},{""id"":""112"",""name"":""112"",""data"":""0,3048"",""children"":[{""id"":""1121"",""name"":""1121"",""data"":""0,3048"",""children"":[]},{""id"":""1122"",""name"":""1122"",""data"":""0,3048"",""children"":[]}]}]},{""id"":""12"",""name"":""12"",""data"":""0,3048"",""children"":[]},{""id"":""13"",""name"":""13"",""data"":""0,3048"",""children"":[{""id"":""131"",""name"":""131"",""data"":""0,3048"",""children"":[{""id"":""1311"",""name"":""1311"",""data"":""0,3048"",""children"":[]}]},{""id"":""132"",""name"":""132"",""data"":""0,3048"",""children"":[]}]},{""id"":""14"",""name"":""14"",""data"":""0,3048"",""children"":[]}]}"
'ScriptManager.RegisterStartupScript(UpdatePanel1, Me.GetType(), "GraphFunction", "drawChart()", True)
If Page.IsPostBack AndAlso GraphNames.SelectedIndex >= 0 Then
SetGraph(GraphNames.SelectedIndex)
End If
If Not Page.IsPostBack Then
OnOffCheck.Items.Add("On")
OnOffCheck.Items.Add("Off")
Menu1.Items(0).Selected = True
StaticService.CreateErrorDictionary()
RunDBInfo()
Label4.Text = ""
If Page.IsPostBack AndAlso GraphNames.SelectedIndex >= 0 Then
SetGraph(GraphNames.SelectedIndex)
End If
End If
Dim graphString = jSerializer.Serialize(ObjList)
ScriptManager.RegisterStartupScript(UpdatePanel1, Me.GetType(), "GraphFunction", "drawChart('" & graphString & "')", True)
End Sub
Javascript代码:
function drawChart(graphInput) {
// var data = google.visualization.arrayToDataTable([
// ['X', 'Points', 'Line'],
// [3, 3.5, 1],
// [4, 5.5, 2],
// [4, 5, null],
// [6.5, 7, 3],
// [8, 12, 4],
// [11, 14, 5]
// ]);
// alert(1);
// var data = JSON.parse('<%=jSerializer.Serialize(ObjList)%>');
alert(graphInput);
var data = JSON.parse(graphInput);
alert(data);
dLen = data.length;
var tabledata = new google.visualization.DataTable();
...
有什么问题吗?我怎样才能在 javascript 中正确接收数据?
graphString
未初始化为字符串。您没有指定类型,因此假定为 Object
。 jSerializer.Serialize(ObjList)
实际上 return 是一个字符串吗?如果是这样,请尝试将 graphString
初始化为正确的字符串:
Dim graphString As String = jSerializer.Serialize(ObjList)
.
我在 javascript 上遇到了一些问题。我给js发送参数发送数据是:
即将到来的javascript是:
c#代码:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' JSonID.Value = "{""id"":""1"",""name"":""1"",""data"":""0,3048"",""children"":[{""id"":""11"",""name"":""11"",""data"":""0,3048"",""children"":[{""id"":""111"",""name"":""111"",""data"":""0,3048"",""children"":[]},{""id"":""112"",""name"":""112"",""data"":""0,3048"",""children"":[{""id"":""1121"",""name"":""1121"",""data"":""0,3048"",""children"":[]},{""id"":""1122"",""name"":""1122"",""data"":""0,3048"",""children"":[]}]}]},{""id"":""12"",""name"":""12"",""data"":""0,3048"",""children"":[]},{""id"":""13"",""name"":""13"",""data"":""0,3048"",""children"":[{""id"":""131"",""name"":""131"",""data"":""0,3048"",""children"":[{""id"":""1311"",""name"":""1311"",""data"":""0,3048"",""children"":[]}]},{""id"":""132"",""name"":""132"",""data"":""0,3048"",""children"":[]}]},{""id"":""14"",""name"":""14"",""data"":""0,3048"",""children"":[]}]}"
'ScriptManager.RegisterStartupScript(UpdatePanel1, Me.GetType(), "GraphFunction", "drawChart()", True)
If Page.IsPostBack AndAlso GraphNames.SelectedIndex >= 0 Then
SetGraph(GraphNames.SelectedIndex)
End If
If Not Page.IsPostBack Then
OnOffCheck.Items.Add("On")
OnOffCheck.Items.Add("Off")
Menu1.Items(0).Selected = True
StaticService.CreateErrorDictionary()
RunDBInfo()
Label4.Text = ""
If Page.IsPostBack AndAlso GraphNames.SelectedIndex >= 0 Then
SetGraph(GraphNames.SelectedIndex)
End If
End If
Dim graphString = jSerializer.Serialize(ObjList)
ScriptManager.RegisterStartupScript(UpdatePanel1, Me.GetType(), "GraphFunction", "drawChart('" & graphString & "')", True)
End Sub
Javascript代码:
function drawChart(graphInput) {
// var data = google.visualization.arrayToDataTable([
// ['X', 'Points', 'Line'],
// [3, 3.5, 1],
// [4, 5.5, 2],
// [4, 5, null],
// [6.5, 7, 3],
// [8, 12, 4],
// [11, 14, 5]
// ]);
// alert(1);
// var data = JSON.parse('<%=jSerializer.Serialize(ObjList)%>');
alert(graphInput);
var data = JSON.parse(graphInput);
alert(data);
dLen = data.length;
var tabledata = new google.visualization.DataTable();
...
有什么问题吗?我怎样才能在 javascript 中正确接收数据?
graphString
未初始化为字符串。您没有指定类型,因此假定为 Object
。 jSerializer.Serialize(ObjList)
实际上 return 是一个字符串吗?如果是这样,请尝试将 graphString
初始化为正确的字符串:
Dim graphString As String = jSerializer.Serialize(ObjList)
.