0

I have a json function as shown below in aspx.

   <script type="text/javascript">
   var TotalData = new Array();    
    function Show()
    {

    TotalData[0] = "Jan, 25";
    TotalData[1] = "Feb, 42";
     alert("hai");
    }

I want to assign the same array from c# page load and need to call the js function Show(). How to do it?

 protected void Page_Load(object sender, EventArgs e)
{ 

 //string[,] TotalData = new string[2, 2] { { "Jan", "25" }, { "Feb", "42" } };
//string serializedNumbers = (new JavaScriptSerializer()).Serialize(TotalData);

 //need to assign TotalData array here.instead in javascript.
 ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:Show(); ", true);
}

3 Answers 3

1

If you want to set the array from the code behind, you need to manually build up the array as a JavaScript string and then register your script with the ClientScript.RegisterStartupScript.

Something like this should get you started:

List<string> totalData = new List<string>();
totalData.Add("Jan, 25");
totalData.Add("Feb, 42");

StringBuilder sb = new StringBuilder();
sb.Append("<script>");
sb.Append("var TotalData = new Array();");
foreach(string str in totalData)
{
  sb.Append("TotalData.push('" + str + "');");
}
sb.Append("</script>");

ClientScript.RegisterStartupScript(this.GetType(), "InitTotalData", sb.ToString());

InitTotalData is the script name as identified by the ClientScriptManager. You can print the contents of the JavaScript array like so:

alert(TotalData.join());

This will output: Jan, 25,Feb, 42

If you want to embed the array directly in the ASPX page, you can do something like the following:

<script type="text/javascript">
<% var totalDataCSharp = new List<string>() { "Jan, 25", "Feb, 42" } %>
<% var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); %>
var TotalData = <%= serializer.Serialize(totalDataCSharp) %>;
</script>

Note, the totalDataCSharp list can be maintained from the code behind.

Sign up to request clarification or add additional context in comments.

2 Comments

What is InitTotalData? function name? How can I see each string element in javascript alert box?
I've updated my answer to address your additional questions.
1

First put you data in a sortedlist like below:

    SortedList<int,string> TotalData = new SortedList<int,string>();
    TotalData .Add("Jan", "12");
    TotalData .Add("Apr", "15");
    TotalData .Add("Feb", "23");
    TotalData .Add("Dec", "19");
    TotalData .Add("Aug", "21");
var TotalData=JsonConvert.SerializeObject(TotalData );

include Newtonsoft like:

using Newtonsoft.Json;

you may need to install the package Just follow thw step below:

PM> Install-Package Newtonsoft.Json

2 Comments

@user2431727 Try it.
var TotalData contains JSON object you can pass this as a parameter . also try this on stackoverflow.com/questions/3464498/…
0

You do not need two denominational array as you need array to string "Jan, 25", "Feb, 42" as elements of string array. There would be little change in your code that is shown below.

On server side C#

string[] TotalData = new string[2] { "Jan, 25", "Feb, 42" };
string serializedNumbers = (new JavaScriptSerializer()).Serialize(TotalData);
ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:show("+ serializedNumbers + "); ", true);

On Client Side

function show(arr)
{
    for (l = 0; l < arr.length; l++)
    {
        console.log("arr[" + l + "] " + arr[l]);
    }
}

2 Comments

My question is how to assign this values strArray and get it in json .
Your required output is simple array of string, see me updated answer.

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.