Sunday, March 11, 2012

passing name/value arrays with ajax

You can′t write client scripts at runtime when the process is in assynchronous postback. So, the way that I get is to create a HiddenField. At server side, you write the value property. At client, you read it. Before postback assynchornous (when Page.IsPostback = false), you must to register a script client to pool the HiddenField (using timer function like as "window.setInterval"). When the value of it is different of "", you run something and turn the value to "" again. The secret is in to invoke the client scripts from server through of a changing of value of a control visible from server.

Excuse for my way of writing. I don′t speak English so much...

I'm sorry, that is not at all what I was asking

I have a name/value collection:
"key2"=>"some other value"

I am creating it in jscript then sendding it via an ajax call to a function on the server which will re-form it (into a NameValueCollection I guess, but I don't really care) and work with it. I am asking if there is a way to serialize this for sending it over. For example, at present I am going to transform it into the following:
<NameValueCollection><add name="key1" value="value1" /><add name="key2" value="some other value" /></NameValueCollection
Of course I need to write client-side and server side functions to break it down/rebuild it. This is a very common task so there must be a built in way to do this. Right?
So that's it, no postback is being triggered, no form, just an object being turned into a string and being sent back and forth.
Let me try one more self-bump.
What about JSON? I can see the AJAX.JSON namespace in the object browser, but can't find any documentation on how to use the classes within it anywhere!

I don′t know what is that class. About deserializarion of NameValueCollection for JavaScript, I don′t think that some built′in component exists. Cause a script used by AJAX must be executed for all browsers. Maybe there are some developement software of some enterprise in internet to do that. Is possible make something like that, but you lost much time of working. I recommend to you read XML serialized in client to get the values from server side.

On more time, excuse me for my English.

Something pretty easy could be (client):

function test()
var myObj = { propertyIndex:"Property Value"};
MyService(myObj, tst);


function tst(result)

And your web service:

publicstring Test(ListDictionary tst)
return tst["propertyIndex"].ToString();

If you wanted to manually serialize, say a JavaScript array to JSON text you could invoke the following ASP.NET AJAX client-side method: Sys.Serialization.JavaScriptSerializer.serialize() and from the server to deserialize you could do something like:

JavaScriptSerializer jsSerializer =newJavaScriptSerializer();
Dictionary<string,object> dicObj = jsSerializer.DeserializeObject(jsonText)asDictionary<string,object>;

Something like that should get you going pretty quick. Hope this helps.

Thanks matthew, I ended up writting SerializeObjectToXml() and getNameValueCollectionFromXml() functions. Its just shocking to me that a way to do this was not already built in as much as passing information via associative arrays is used in web programming.

I am not sure if there is special things needs to do for passing Dictionary. Check out the following:

[WebMethod()]public bool SendWeathers(Dictionary<string,float> weathers) {foreach (KeyValuePair<string,float> itemin weathers) { Console.WriteLine("{0} : {1}", item.Key, item.Value); }return true; }

To Call:

var weathers =new Object(); weathers['Dhaka'] = 32.2; weathers['Chittagong'] = 36.7; weathers['Khulna'] = 34.5; weathers['Rajshai'] = 35; SimpleService.SendWeathers(weathers, function(result) { alert(result); } );

Also read the following:

Thank you very much, that is the sort of thing I wanted (though I have written my own code by now). A question: Does this method work for ASP.NET 1.1 or just 2.0?
I like the idea of running that query a webservice, currently it is being achieved by calling a function with the Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)] tag, but if I had been the one to design the system I would certainly have done it through web services.

Sorry, It only works with Ajax Framework not with others.

No comments:

Post a Comment