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

by APIJunkie 2/13/2008 10:14:00 AM
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

 

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.NET | AJAX | Troubleshoot | JSON | JavaScript

How to use json.js client side JavaScript file inside an AJAX.NET project

by APIJunkie 2/11/2008 8:12:00 AM
Sometimes it is necessary to mix json.js functions like toJSONString() with client script generated by the AJAX.NET library.

If you include the json.js file in your client script you might receive enumValueNotInteger error.

A little digging around landed me on the following helpful discussion at http://forums.asp.net/t/1077290.aspx

Steve Marx raises several options to solve the problem in the above discussion.

To solve my problem I used the script manager Scripts property option.

Example:

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

<Scripts>

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

</Scripts>

</ajaxToolkit:ToolkitScriptManager>

Note if you are using master pages or need json.js script inside custom controls you can use the script manager proxy Scripts property instead.

Example:

<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">

<Scripts>

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

</Scripts>

</asp:ScriptManagerProxy>

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.NET | AJAX | JavaScript | JSON

Powered by BlogEngine.NET 1.2.0.0
Theme by Mads Kristensen

About the author

Name of author

My name is Bacon…James Bacon.

I am an API wars veteran I was wounded by x86 assembly, recovered and moved on to C. I am currently stuck in C++ and sniffing .NET.

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.

E-mail me Send mail


Calendar

<<  November 2008  >>
MoTuWeThFrSaSu
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

View posts in large calendar

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in