Suppose I have got a form where there are lots of labels,one textbox and a button.
The textbox is used for submitting the CustomerID and when the button is presssed I want to fill the entire labels with different info about the customer.
I want to use WebMethods for that to avoid the postback.
WebMethod which I have used, return only single string.
the web method would need to be set to return either the data in the single string but for example as comma-separated string (no need to change the signature), or then just changing the return type to be a custom class having specifying what needs to /can
be returned. E.g it means a change at the web service.
Simply define a class that contains all the fields you want to return, then fill an instance of that class with the information about the customer, then return the instance. That's all there is to it.
public class MultipleValues {
public int IntegerValue {get;set;}
public string StringValue1 {get;set;}
public string StringValue2 {get;set;}
}
As you can see, you did not need an example. You needed to create a class, create an instance of the class, fill in the properties, then return the instance.
And, with any and all due respect, if you don't know how to do the above, then you should not program computers until you
do know how to do these trivial things. This is pretty much "programming 101" stuff. If you can't do it yet, you should say so, and go back to basics, rather than asking for samples that you will not understand.
Ok, your respect is accepted, and I will tell you.
The answer is that you will never send a C# class to a JavaScript program over web services; nor to a Java program, nor to a PHP script. In fact, you will never send such a class even to another C# program!
What happens instead, is that the contents of the class will be serialized into XML or JSON, and the serialized data is what will be sent to the other side. In the case of JSON, the other side needs no additional information in order to interpret
the results: JSON can be processed by a JavaScript program quite directly (eval, I believe). In the case of XML, additional metadata is necessary, but this is provided by the WSDL, so it's no big deal. The other side will deserialze the XML into objects.
The neat thing is that the objects that the data gets deserialzed into will be objects of the correct platform - by definition. The deserialization is performed by code written in the correct platform, so naturally, it will produce its own objects! The other
side will never even know what platform sent the data - all the other side will see is serialized data, and possibly metadata describing the structure of the serialized data.
So, no magic, but a pretty neat trick just the same.
See
Basics: How Web Services Work for another description of this. That post is purely in terms of XML; I should update it for JSON.
Finally: notice that I still haven't written you an example, and I don't think any example could have conveyed the information above.
akshaycjoshi
Member
2 Points
15 Posts
Return multiple values WebMethods
Jun 11, 2009 05:42 AM|LINK
Suppose I have got a form where there are lots of labels,one textbox and a button.
The textbox is used for submitting the CustomerID and when the button is presssed I want to fill the entire labels with different info about the customer.
I want to use WebMethods for that to avoid the postback.
WebMethod which I have used, return only single string.
How would i get multiple strings ?
Thanks !
WebMethods
joteke
All-Star
46284 Points
6896 Posts
ASPInsiders
MVP
Re: Return multiple values WebMethods
Jun 14, 2009 07:21 PM|LINK
Hi,
the web method would need to be set to return either the data in the single string but for example as comma-separated string (no need to change the signature), or then just changing the return type to be a custom class having specifying what needs to /can be returned. E.g it means a change at the web service.
Teemu Keiski
Finland, EU
johnwsaunder...
Star
11262 Points
1981 Posts
Re: Return multiple values WebMethods
Jun 14, 2009 09:49 PM|LINK
Simply define a class that contains all the fields you want to return, then fill an instance of that class with the information about the customer, then return the instance. That's all there is to it.
akshaycjoshi
Member
2 Points
15 Posts
Re: Return multiple values WebMethods
Jun 15, 2009 02:45 AM|LINK
Joteke and johnwsaunders3, Can both of you please write an example for me.
Thanks
johnwsaunder...
Star
11262 Points
1981 Posts
Re: Return multiple values WebMethods
Jun 15, 2009 01:34 PM|LINK
Sir, with all due respect, if you don't know how to create a class with multiple properties, then an example won't help you.
akshaycjoshi
Member
2 Points
15 Posts
Re: Return multiple values WebMethods
Jun 16, 2009 02:45 AM|LINK
I know how to create a class.
It's just the difference in platforms.
Will a class "Employee" in c# be understood by javascript ?
Write me an example please.
johnwsaunder...
Star
11262 Points
1981 Posts
Re: Return multiple values WebMethods
Jun 16, 2009 02:50 AM|LINK
Here's an example:
public class MultipleValues { public int IntegerValue {get;set;} public string StringValue1 {get;set;} public string StringValue2 {get;set;} }As you can see, you did not need an example. You needed to create a class, create an instance of the class, fill in the properties, then return the instance.
And, with any and all due respect, if you don't know how to do the above, then you should not program computers until you do know how to do these trivial things. This is pretty much "programming 101" stuff. If you can't do it yet, you should say so, and go back to basics, rather than asking for samples that you will not understand.
akshaycjoshi
Member
2 Points
15 Posts
Re: Return multiple values WebMethods
Jun 16, 2009 09:43 AM|LINK
I want to know how a class written in c# would be understood by javascript ?
Remember, I am asking with with respect, more than anyone else on this planet.
johnwsaunder...
Star
11262 Points
1981 Posts
Re: Return multiple values WebMethods
Jun 16, 2009 10:56 AM|LINK
Ok, your respect is accepted, and I will tell you.
The answer is that you will never send a C# class to a JavaScript program over web services; nor to a Java program, nor to a PHP script. In fact, you will never send such a class even to another C# program!
What happens instead, is that the contents of the class will be serialized into XML or JSON, and the serialized data is what will be sent to the other side. In the case of JSON, the other side needs no additional information in order to interpret the results: JSON can be processed by a JavaScript program quite directly (eval, I believe). In the case of XML, additional metadata is necessary, but this is provided by the WSDL, so it's no big deal. The other side will deserialze the XML into objects.
The neat thing is that the objects that the data gets deserialzed into will be objects of the correct platform - by definition. The deserialization is performed by code written in the correct platform, so naturally, it will produce its own objects! The other side will never even know what platform sent the data - all the other side will see is serialized data, and possibly metadata describing the structure of the serialized data.
So, no magic, but a pretty neat trick just the same.
See Basics: How Web Services Work for another description of this. That post is purely in terms of XML; I should update it for JSON.
Finally: notice that I still haven't written you an example, and I don't think any example could have conveyed the information above.
akshaycjoshi
Member
2 Points
15 Posts
Re: Return multiple values WebMethods
Jun 23, 2009 03:24 AM|LINK
Still, my respect will touch sky if u provide me an example or atleast a link containing the example I want.
That's coz I want to instantly solve my problem for now,I know that is difficult and I wont understand that very quickly.
Thanks