5,136,034 members and growing! (12,438 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Web Parts     Intermediate License: The Code Project Open License (CPOL)

A base SharePoint web part for AJAX in ASP.NET 3.5

By Albert Lu

A base SharePoint web part to support AJAX in ASP.NET 3.5
C# (C# 1.0, C# 2.0, C# 3.0, C#), .NET (.NET, .NET 3.5), ASP.NET, Ajax, Dev

Posted: 9 Apr 2008
Updated: 12 May 2008
Views: 3,562
Announcements



Search    
Advanced Search
Sitemap
2 votes for this Article.
Popularity: 1.10 Rating: 3.67 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 50.0%
3
1 vote, 50.0%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

This article describe an abstract web part class developed for the SharePoint 2007 (WSS 3.0 or MOSS 2007) platform. The web part contains the logic to address some known conflicts between SharePoint and ASP.NET AJAX and is designed as the base class for all AJAX enabled SharePoint web parts.

Background

With SharePoint (WSS 3.0 or MOSS 2007) SP1, AJAX is officially supported. However, there are still lots of manual configuration needs to be performed for AJAX to work in the SharePoint environment. Basically you can create an ASP.NET web application targeting .NET framework 3.5 and merge the AJAX related entries into the web.config of your SharePoint application. However, this is not enough. In the MSDN article titled Walkthrough: Creating a Basic ASP.NET AJAX-enabled Web Part, a technique is introduced to fix the conflict between SharePoint and the ASP.NET AJAX UpdatePanel. This article provides a good starting point for developing AJAX enabled SharePoint web part. However, the technique described in the article had its limitations as well. I will go over these limitations below and explained how they can be addressed.

Using the code

ASP.NET AJAX requires one instance, and only one instance of the ScriptManager on any page. There are several ways to include the ScriptManager in a SharePoint web part page. One thing you can do is to modify the master page. Another common technique is to detect if an instance of the ScriptManager already exist, and create one on demand if it does not exist. I like the later approach as it is more flexible than modifying the master page, which affects all pages regardless if AJAX is used in the page. After all, there are 3rd party AJAX libraries that are not currently compatible with ASP.NET AJAX, and you may not have full control on all the contents that appears on a portal.

After reviewing the life cycles of an ASP.NET page one more time, I decided to place the logic that creates an instance of the ScriptManager inside the OnInit event, and that seems to work pretty well.

Another issue comes with the "EnsurePanelFix" logic, as it too, should not be registered more than once. By creating a common base class for AJAX enabled web part, and register the script using the type of the base web part, the problem can be solved. This is especially good as not only the base web part promotes code reuse, it also fixes problems!

The full code for the web part is included below:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Web.UI.WebControls; 
    public abstract class AjaxBaseWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            // Register the ScriptManager
            ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
            if (scriptManager == null)
            {
                scriptManager = new ScriptManager();
                scriptManager.ID = "ScriptManager1";
                scriptManager.EnablePartialRendering = true;
                Controls.AddAt(0, scriptManager);
            }
        }

        protected override void CreateChildControls()
        {
            // Add fix according to http://msdn2.microsoft.com/en-us/library/bb861877.aspx
            EnsurePanelFix();
        }

        private void EnsurePanelFix()
        {
            if (this.Page.Form != null)
            {
                String fixupScript = @"
     _spBodyOnLoadFunctionNames.push(""_initFormActionAjax"");
     function _initFormActionAjax()
     {
       if (_spEscapedFormAction == document.forms[0].action)
       {
         document.forms[0]._initialAction = 
         document.forms[0].action;
       }
     }
     var RestoreToOriginalFormActionCore = 
       RestoreToOriginalFormAction;
     RestoreToOriginalFormAction = function()
     {
       if (_spOriginalFormAction != null)
       {
         RestoreToOriginalFormActionCore();
         document.forms[0]._initialAction = 
         document.forms[0].action;
       }
     }";
                ScriptManager.RegisterStartupScript(this,
                  typeof(AjaxBaseWebPart), "UpdatePanelFixup",
                  fixupScript, true);
            }
        }
    }

        

Points of Interest

Despite the official support of AJAX in SP1 of SharePoint 2007, it still take lots of effort to start using AJAX in the SharePoint environment. Maybe the next service patch for Visual Studio 2008 will provide the same support for SharePoint development as the support we get for ASP.NET 3.5 AJAX? Let's keep our fingers crossed.

History

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Albert Lu


Albert has been developing software solutions on windows and web for over 10 years. Most recently he has been creating software using SharePoint and BPM systems.
Location: United States United States

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
Subject  Author Date 
Generalthis code does not work even the Walkthrough: Creating a Basic ASP.NET AJAX-enabled Web Part does not work eithermemberMember 39989191:43 24 Apr '08  
GeneralRe: this code does not work even the Walkthrough: Creating a Basic ASP.NET AJAX-enabled Web Part does not work either [modified]memberjoebrockhaus10:02 12 May '08  
GeneralRe: this code does not work even the Walkthrough: Creating a Basic ASP.NET AJAX-enabled Web Part does not work eithermemberAlbert Lu11:49 12 May '08  
GeneralRe: this code does not work even the Walkthrough: Creating a Basic ASP.NET AJAX-enabled Web Part does not work eithermemberMember 399891922:37 12 May '08  
GeneralRe: this code does not work even the Walkthrough: Creating a Basic ASP.NET AJAX-enabled Web Part does not work eithermemberMember 399891922:41 12 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 May 2008
Editor:
Copyright 2008 by Albert Lu
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project