Reputation: 907
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);
}
Upvotes: 0
Views: 862
Reputation: 9789
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.
Upvotes: 1
Reputation: 148150
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]);
}
}
Upvotes: 0
Reputation: 664
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
Upvotes: 1