DNN Blog

Mar 17

Posted by: Michael Washington
3/17/2007  RssIcon

This article will cover what is needed to implement web services for use with ASP.NET AJAX in the DotNetNuke Framework. It uses code from the Microsoft ASP.NET AJAX site (http://ajax.asp.net/docs/tutorials/ConsumingWebServicesWithAJAXTutorial.aspx) that was converted to use as a DotNetNuke module.

Also, see this post: Creating A DotNetNuke Module using ASP.NET AJAX for an overview of AJAX functionality with DotNetNuke and Creating Secure DotNetNuke ASP.NET AJAX Web Services for creating secure web services.

The Problem

Looking at the Microsoft ASP.NET AJAX tutorial, you will see example code such as this:


 
           path="~/WebServices/SimpleWebService.asmx" />
 

The problem is that you cannot use this code with a DotNetNuke module. If you do you will get this error:


Unhandled error loading module.
DotNetNuke.Services.Exceptions.ModuleLoadException: Unhandled Error Adding Module to ContentPane ---> System.InvalidOperationException: Only one instance of a ScriptManager can be added to the page. at System.Web.UI.ScriptManager.OnInit(EventArgs e) at System.Web.UI.Control.InitRecursive(Control namingContainer) at System.Web.UI.Control.InitRecursive(Control namingContainer) at System.Web.UI.Control.InitRecursive(Control namingContainer) at System.Web.UI.Control.InitRecursive(Control namingContainer) at System.Web.UI.Control.InitRecursive(Control namingContainer) at System.Web.UI.Control.AddedControl(Control control, Int32 index) at System.Web.UI.ControlCollection.Add(Control child) at DotNetNuke.UI.Skins.Skin.InjectModule(Control objPane, ModuleInfo objModule, PortalSettings PortalSettings) --- End of inner exception stack trace ---

The reason is that the DotNetNuke Framework injects the Script manager for you and only one script manager can be placed on a page at one time.

The Solution

In the example code (VB / C#), there is a DotNetNuke user control (View.ascx) that has a JavaScript file (CallWebServiceMethods.js) and a web service (WebService.asmx).

First, in the Page_Load method, obtain an instance of the ScriptManager:

VB:

Dim objScriptManager As ScriptManager = ScriptManager.GetCurrent(Me.Page)

C#:

ScriptManager objScriptManager = ScriptManager.GetCurrent(this.Page)

and then set your properties:

VB:

Dim objServiceReference As ServiceReference = New ServiceReference
objServiceReference.Path = "~/DesktopModules/AjaxWebService/WebService.asmx"
objScriptManager.Services.Add(objServiceReference)

Dim objScriptReference As ScriptReference = New ScriptReference
objScriptReference.Path = "~/DesktopModules/AjaxWebService/CallWebServiceMethods.js"
objScriptManager.Scripts.Add(objScriptReference)

C#:

ServiceReference objServiceReference = new ServiceReference();
objServiceReference.Path = @"~/DesktopModules/AjaxWebService/WebService.asmx";
objScriptManager.Services.Add(objServiceReference);

ScriptReference objScriptReference = new ScriptReference();
objScriptReference.Path = @"~/DesktopModules/AjaxWebService/CallWebServiceMethods.js";
objScriptManager.Scripts.Add(objScriptReference);

Secure Web Services

You may notice that the web service included with the sample code does not have any security and will return the same data no mater what portal the module is used on. This would not be very usable in a real world application. Most of the issues of providing security and making a web service portal specific have been implemented in the IWEB project. The code is free and it even provides a configuration screen for your web methods. See: Creating Secure DotNetNuke ASP.NET AJAX Web Services

Download the sample code here [VB] [C#]

Tags:
Categories:

8 comment(s) so far...


Re: Creating A DotNetNuke ASP.NET AJAX Web Service

another great article, michael!

By rudgr on   3/22/2007

Re: Creating A DotNetNuke ASP.NET AJAX Web Service

This only applies to DotNetNuke 4.5.0 or higher. It should be released by April 2007.

By AdefWebserver on   3/22/2007

Re: Creating A DotNetNuke ASP.NET AJAX Web Service

My 2 cents...

No... this applies to ALL DNN 4.x versions :)

Also... the part of the code where you inject the script references on the server is not needed because you have a ScriptManagerProxy control which is made for just this purpose... you add it to a page, and add script, service and other references declaratively.

Cheers!

Vladan Strigo
http://vladan.strigo.net

By vladan on   3/22/2007

Re: Creating A DotNetNuke ASP.NET AJAX Web Service

forgot to finish the thought... ScriptManagerProxy is made for a scenario where you have ScriptManager in a master page file, parent control or something similar, and it's purpose is to allow you to go besides the problem of having one ScriptManager per page.

Vladan Strigo
http://vladan.strigo.net

By vladan on   3/22/2007

Re: Creating A DotNetNuke ASP.NET AJAX Web Service

Yes, you can use the ScriptManagerProxy control. You will usually want to check to see if AJAX is installed before you add the references to the script manager so you may not want to add the references declaratively (yes you could also disable them in the code behind). There are different ways of course, however, the above code will work and is a smaller amount of code. Also, this particular code sample will not run on previous versions of DotNetNuke because the DotNetNuke.Framework.AJAX namespace does not exist. I am addressing the issue of people trying to use code that was posted on the Microsoft site.

By AdefWebserver on   3/23/2007

Re: Creating A DotNetNuke ASP.NET AJAX Web Service

Ok, It's good you've explained it so that people don't have unnecessary confusions.

Cheers!

Vladan Strigo
http://vladan.strigo.net

By vladan on   3/24/2007
Gravatar

Re: Creating A DotNetNuke ASP.NET AJAX Web Service

Thank you!

Another brilliantly simple thing made stupidly complicated by DNN finally solved by an old post.

Thanks again...

By schmakt on   11/10/2011
Gravatar

Re: Creating A DotNetNuke ASP.NET AJAX Web Service

Please can someone help me out to working with ajax in dnn 6, i am in real trouble right now my project deadline coming soon. and my page function depend on the ajax. i did not want to add the web services in my page. i just want use simple ajax calander extender. i installed the ajax control kit and its configure in my environment. i am using chirs template. i just create one text box in the view.ascx file and then on page load i check the ajax is installed and then register ajax server. i check in the debug its register the server. but when i click on the textbox its does not shows the calander. i put my query on the dotnetnuke but no body help me their. please help me what i have to do its work.

here is my page code








and here is page load function

if (DotNetNuke.Framework.AJAX.IsInstalled())
{
DotNetNuke.Framework.AJAX.RegisterScriptManager();
}

By Shahid Majeed on   11/10/2011
Attend A Webinar
Free Demo Site
Download DotNetNuke Professional Edition Trial
Have Someone Contact Me

Like Us on Facebook Join our Network on LinkedIn Follow DNN Corporate on Twitter Follow DNN on Twitter

Advertisers

Sponsors

DotNetNuke Corporation

DotNetNuke Corp. is the steward of the DotNetNuke open source project, the most widely adopted Web Content Management Platform for building web sites and web applications on Microsoft .NET. Organizations use DotNetNuke to quickly develop and deploy interactive and dynamic web sites, intranets, extranets and web applications. The DotNetNuke platform is available in a free Community and subscription-based Professional and Enterprise Editions with an Elite Support option. DotNetNuke Corp. also operates the DotNetNuke Store where users purchase third party apps for the platform.