Tuesday, 15 March 2011

c# - Asp.net pass data from code behind to Javascript and call array -


I have an asp.net project that uses some analytics controls to see different data. Controls are populated by JavaScript functions such as:

  Morris.bar ({element: 'hero-bar', data: [{device: '1', sales: 136} {Device: '3G', Sales: 1037}, {Device: '3G', Sales: 275}, {Device: '4', Sales: 380}, {Device: '4S', Sales: 655} , 'Device', ykeys: ['sales'], labels: [' cell '], barratio: 0.4, xLabelMargin: 10, hidden:' auto ', {device:' 5 ', sells: '5', '5', sells: 1571}], xkey: , Bar collar: ["# 3d88ba"]});  

What I have to do, pass some values ​​from the code behind it and populate the 'data: []' array (where I get the actual values ​​from the database).

Can anybody help me go about crossing data, and in what format will it be filled? How do I add variables to javascript?

Preference in item (

Can I store it as a string array, on which my code in the collection behind a foreach The loop can be run: / here "myDataFromCodeBehind" is called sb.Append ("{Device: xVariable, Sales: yVariable},"); }

and then call the string array from javascript

  var myData = "& lt;% = myDataFromCodeBehind%>"; Morris.bar ({element: 'Hero-Bar', Data: [Myadata], // etc.  

Or will not it work? Something tells me that I am wrong about it . I'm new to JavaScript and I'm not sure this will work.

First of all, your definition is JSON The object is used as a client:

  var YourBarParam = {element: 'hero-bar', data: [[device: '1', sales: 136}, {device: ' 3G ', Sales: 1037}, {Device:' 3G ', Sells: 275} {Device: '4', Sells: 380}, {Device: '4S', Sales: 655}, {Device: '5', Sales: 1571}], xkey: 'Device', ykeys: [' Sales'], Labels: ['cell'], barratio: 0.4, Xlabel margin: 10, hiding: 'auto', bar collar: ["# 3d88ba"]}  

Next page Go to and describe your class to the JSON object.

  public class data {public string device {get; set;} sells public int {get; set;}} public class RootObject { Public string element {get} set;} Arwajnik List & lt; Data & gt; Data {Received; Set; } Public string xkey {get; Set; } Public listing & lt; String & gt; Ykeys {get; Set; } Public listing & lt; String & gt; Label {get; Set; } Public Double Bar Righteous {get; Set; } Public int xLabelMargin {get; Set; } Hide public strings {get; Set; } Public listing & lt; String & gt; Bar colors {receive; Set;  

Now define the public page method or property behind the code where you will store object or JSON formated objects back.

Regenerate your object using the newtonsoft nuget package:

public static string yourObject = string.Empty; Public Stabilized String getBarObject () {Root object is your Barapuram = new root object (); Product.element = "hero-bar"; Product.data = new list & lt; Datam & gt; (); Mydatun = New Date (); Mydatun.device = "1"; Mydatun.sells = "whatever" product.data.add (mydatun); ... // and so on ... yourObject = JsonConvert.SerializeObject (YourBarParam); Return your object; }

In the page markup client script section

  var myData = "& lt;% # getBarObject ()%>";  

or

  var myData = "& lt;% = yourObject% & gt;";  

If you only need an array of data, then it

  yourObject = JsonConvert.SerializeObject (YourBarParam.data);  

No comments:

Post a Comment