David Rasuli
David Rasuli

Reputation: 832

Recieve datatable from code behind to Javascript two demintion array?

I have an application based on google map, in which I need to use a javascript matrix (2d array) for the map's parameters.

I have a datatable with the information at my code behind file:

..query code, getting value from the db..
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adp.Fill(dt);

How do I pass this datatable values into a js two denominational array?

Thanks

Upvotes: 0

Views: 5568

Answers (3)

MACMAN
MACMAN

Reputation: 1971

Why not use Jquery Ajax? With Jquery Ajax you can bring data from server side to client side.

Upvotes: 0

Symeon Breen
Symeon Breen

Reputation: 1541

An easy way is on the aspx page to have where appropriate

<script type="text/javascript>
    var mapData = <%=mapData%>
</script>

Then in your code behind create a public string called mapData and assign it as a json string. e.g.

mapData = "{({name:'blah',lat:10.223,long:57.899},{etc})};
  • obviously loop through your dataset to create the string.

You can then use it in your javascript on the page

Upvotes: 1

las
las

Reputation: 196

You can use Ajax for retrieve the data table as an array from the server.

public ArrayList ConvertDT(ref DataTable dt)
{
   ArrayList converted = new ArrayList(dt.Rows.Count);
    foreach (DataRow row in dt.Rows)
            converted.Add(row);
    return converted;
    }

then array list convert into array and send back as a response to the client

http://www.dreamincode.net/forums/topic/91826-datatable-to-array/

Upvotes: 0

Related Questions