1

i'm using asp.net web form fx3.5 and i'm trying to get my server-side string array into my javascript. i found a simple example that claims to work but it doesn't for me. temp variable is not recognized in ().Serialize(temp);

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>
3
  • What's in the temp variable? Null? How do you serialize null? Commented Sep 26, 2011 at 23:36
  • Have you tried initialising temp to some dummy data? It may well be erroring as temp hasn't be initialized Commented Sep 27, 2011 at 0:04
  • Is this a JavaScript question? I don't see any JavaScript. (JSON isn't JavaScript.) Commented Sep 27, 2011 at 0:10

1 Answer 1

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.