2011-09-26 42 views
1

我使用asp.net web表單fx3.5,我試圖讓我的服務器端字符串數組到我的JavaScript。我發現一個簡單的例子,聲稱工作,但它不適合我。臨時變量在().Serialize(temp);從C#字符串數組到javascript示例不工作

Here's the reference article

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ShelterExpress.UserInterface.WebForm1" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<script runat="server" language="c#"> 
    string[] temp; 
    int lengthOfTemp; 

    public string tempJSON = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(temp); 
</script> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 

    </div> 
    </form> 
</body> 
</html> 
+0

什麼是在臨時變量?空值?你如何序列化null? –

+0

你有沒有嘗試初始化一些虛擬數據的溫度?這可能是錯誤的,因爲臨時沒有初始化 –

+0

這是一個JavaScript的問題?我沒有看到任何JavaScript。 (JSON不是JavaScript。) – nnnnnn

回答

0
<script runat="server" language="c#"> 
    string[] temp; 
    int lengthOfTemp; 

    public string tempJSON; 

    protected override void OnLoad(EventArgs e)//you have to initialize your temp and tempJSON in a method 
    { 
     base.OnLoad(e); 
     temp = new string[] { "Hi", ",", "ojlovecd" };//Initialize your temp here 
     tempJSON = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(temp); 
    } 

</script>