tags:

views:

198

answers:

4

I am using jQuery in my web application. There are two fields where I want to pass an array value from code behind using jQuery.

I am using this code for a graph:

var chart2={
  label:names['c2'],
  type:$('select[@name=c2type]').val(),
  color:$('select[@name=c2color]').val(),
  values:getTableCol('c2'),
  stackedOn:names[$('select[@name=c2stack]').val()]};

In values:getTableCol('c2') I need to pass the array value from a code behind page. Right now it's taking its value from a table column but I don't need the table. Here I want to pass a value of form {12,45,45,50,55} at run time. It is changeable.

How can I pass this value?

A: 

You could render the value into a hidden field on the page using the HtmlInputHidden control. Provided you give the field an ID that can be referenced from jQuery you will be able to access it.

Alex Angas
A: 
// selects both table header and table data cells from the third column of #mytable
$('#mytable th:nth-col(3), #mytable td:nth-col(3)');

// same as above but using the nthCol extension function
$('#mytable th, #mytable td).nthCol(3);

copied from http://www.bramstein.com/projects/column/

Tzury Bar Yochay
A: 

You could serialize to JSON and put in the page - JSON is legal JavaScript.

Code-behind:

using System.Web.Script.Serialization;

protected string JsonArray
{
  get {
    var myArray = new int[] { 1, 2, 3 };
    return new JavaScriptSerializer().Serialize(myArray);
  }
}

.aspx:

var chart2 = {
  values: <%= JsonArray %>
};
orip
+1  A: 

I'd use the following inside Page_PreRender:

Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "DeclareMyArray", @"var myArray = ['hello', 'world'];", true);

Then you should be able to do the following:

var chart2={
   label:names['c2'],
   type:$('select[@name=c2type]').val(),
   color:$('select[@name=c2color]').val(),
   values:myArray,
   stackedOn:names[$('select[@name=c2stack]').val()]};
mdresser