Arnkrishn
Arnkrishn

Reputation: 30424

Accessing ASP.NET Server variables in jQuery

In Page_Load() function, I have populated an arraylist and a hashtable. I need to read their values in the $(document).ready(function(){...}.

I am a beginner in terms of ASP.NET and jQuery. Please suggest a way to do it.

cheers

Upvotes: 6

Views: 4263

Answers (4)

David Robbins
David Robbins

Reputation: 10046

JSON.Net will help you serialize the hashtable and array to JSON, and you can store these in a hidden field. Then on the client side you can deserialize the strings to your javascript variables with:

var array = JSON.parse($('#hiddenTextField').val());

Upvotes: 0

Hiyasat
Hiyasat

Reputation: 8926

you can create a page.method in server side and call it in the client side script that return an array of values

by adding 

<scriptManager EnablePageMethods="true">

in the server side you create a function that return an array of values
ex:  GetArrayResult()


  if you intersting in this i will give you the rest of example

Upvotes: 0

ram
ram

Reputation: 11626

you can use PageMethods to make a call to your aspx page from JS. Here is a nice article on how to use page method with jQuery. You can return your "whatever" in your page method. If you want to serialize your server side objects before passing them to the client side, you can serialize them to JSON . Here is an example. You would just read your json data after your ajax call is successful("success: function(msg){//do something}") and update your HTML accordingly

Upvotes: 5

TheVillageIdiot
TheVillageIdiot

Reputation: 40497

you can use Page.ClientScript property to register arrays, scripts etc.

For more information view MSDN

Upvotes: 0

Related Questions