You have the right idea but ASP.NET AJAX has a specific way of creating classes, well in reality JavaScript has it, you should use prototypes to implement your classes, check this link:
http://www.asp.net/AJAX/Documentation/Live/tutorials/ASPNETAJAXAndJavaScriptTutorials.aspx
Here’s an example that should do what you want:
1 Type.registerNamespace("Test");
2
3 Test.MyClass = function() {
4 Test.MyClass.initializeBase(this);
5 }
6 Test.MyClass.prototype = {
7 dispose : function() {
8 Test.MyClass.callBaseMethod(this, "dispose");
9 },
10
11 initialize : function() {
12 Test.MyClass.callBaseMethod(this, "initialize");
13
14 MyWebserice.MyMethod(Function.createDelegate(this, this._myMethodCallbackSucceeded), Function.createDelegate(this, this._myMethodCallbackFailed));
15 },
16
17 _myMethodCallbackSucceeded : function(result) {
18 alert(result);
19 },
20
21 _myMethodCallbackFailed : function(error) {
22 alert(error.get_exceptionType()):
23 }
24 }
25 Test.MyClass.registerClass("Test.MyClass", Sys.Component);
26
27 function pageLoad() {
28 main = new Test.MyClass();
29 main.initialize();
30 }
31
32 if(typeof(Sys) !== "undefined")
33 Sys.Application.notifyScriptLoaded();
Some notes:
- You should use an external .js file for this, load it using the ScriptReference tag of you page’s ScriptManager.
- Notice that you should register a namespace at the beginning
- You can change the name of the namespace (in this case Test) and the name of the class (in this case MyClass) to whatever you want.
- As you can see there’s a pageLoad function, this function gets called when the page loads and calls the initialize function of your class.
- The las two lines of code are for telling ASP.NET AJAX that a new class has been loaded.
- Your webservice gets called in the initialize function of your class.
- As you can see I’m using delegates when asigning the callback functions, this is to maintain the context of the call, otherwise you won’t be able to use “this” in the success callback function.
Hope this helps,
Elias.
If this post helped you please remember to set it as Answer so it can help others.