Avoiding some common errors when consuming web services and calling page methods using ASP.NET AJAX extensions

by APIJunkie 13. February 2008 10:14

I have compiled a small list of things to remember in order to avoid some common errors when using ASP.NET AJAX extensions.

1. Remember to declare your web services using the ScriptService attribute.

Why bother?

If you forget to do that you will not be able to easily access the web service from the client side script. You will be missing the automatically generated client script that will allow you to call the web service from your client script code.

Example:

namespace MyNameSpace

 {

[System.Web.Script.Services.ScriptService]
public class MyService : System.Web.Services.WebService
{
   ...

}

}

More Info:

http://www.asp.net/AJAX/Documentation/Live/mref/T_System_Web_Script_Services_ScriptServiceAttribute.aspx


2. If you wish to access session state in your web service functions remember to set the EnableSession attribute to true in your web method declaration.

Why bother?

If you forget to do this you will not be able to access session variables inside your web service methods.

Example:

namespace MyNameSpace

 {

[System.Web.Script.Services.ScriptService]
public class MyService : System.Web.Services.WebService
{
    [System.Web.Services.WebMethod(EnableSession=true)]

    public void myFunc()
    {
       Session["XVAR"] = 1;
    }

}

}

More Info:

http://msdn2.microsoft.com/en-us/library/system.web.services.webmethodattribute.enablesession.aspx

 

3. If you wish to call static page methods from client script remember to set the EnablePageMethods property of the script manager to true.
 

Why ?

If you forget to do that you will not be able to access the static page methods from the client side script.

Example:

<ajaxToolkit:ToolkitScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">

 

More Info:

http://www.asp.net/AJAX/Documentation/Live/mref/P_System_Web_UI_ScriptManager_EnablePageMethods.aspx

 

4. If you wish to mix json.js client side script with the AJAX .NET client side script, use the script manager/script manager proxy Scripts property.

Why ?

You might receive a client side script error.

Example:

ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">

<Scripts>

   <asp:ScriptReference Path="json.js" />

</Scripts>

</ajaxToolkit:ToolkitScriptManager>

For more info you can read my previous post on the subject -> How to use json.js client side JavaScript file inside an AJAX.NET project

 

Tags:

.NET | AJAX | Troubleshoot | JSON | JavaScript

Comments

2/13/2008 10:31:53 AM #

trackback

Trackback from DotNetKicks.com

Avoiding common errors when consuming web services

DotNetKicks.com

5/27/2010 12:25:16 AM #

Rick Jackson

Hey James Bacon,

Recently I have used AjaxToolKitScriptManager and Set the EnablePageMethods= true but still now its showing ajaxcontroltoolkit is undefined before i used ajaxscriptmanager then it was working but its not working with toolkitscriptmanager any idea why its not working ?


Thanks

Rick Jackson India

Comments are closed

About the author

Name of author

I was first wounded by x86 assembly, recovered and moved on to C. Following a long addiction to C++ and a short stint at rehab I decided to switch to a healthier addiction so I am now happily sniffing .NET and getting hooked on Silverlight.

I am mainly here to ramble about coding, various API’s, Junkies(me especially) and everything else that happens between coders and their significant other.

  James Bacon