Click here to Skip to main content
15,867,453 members
Articles / Web Development / ASP.NET

A base SharePoint Web Part for AJAX in ASP.NET 3.5

Rate me:
Please Sign up or sign in to vote.
3.00/5 (3 votes)
12 May 2008CPOL2 min read 63.2K   20   12
A base SharePoint Web Part to support AJAX in ASP.NET 3.5.

Introduction

This article describes 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 a 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 required 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 Parts. However, the technique described in the article has its limitations as well. I will go over these limitations below and explain 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 modify the Master page. Another common technique is to detect if an instance of the ScriptManager already exists, and create one on demand if it does not. I like the latter 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 third 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 Parts, and registering 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:

C#
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 for AJAX in SP1 of SharePoint 2007, it still needs 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 we get for ASP.NET 3.5 AJAX? Let's keep our fingers crossed.

License

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


Written By
United States United States
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.

Comments and Discussions

 
GeneralViewState issues Pin
eXton23-Feb-09 20:33
eXton23-Feb-09 20:33 
GeneralMistake Pin
gbelzile6-Feb-09 8:07
gbelzile6-Feb-09 8:07 
QuestionStill Postback Issue Pin
Anwarul6-Feb-09 1:52
Anwarul6-Feb-09 1:52 
QuestionSharePoint AJAX simply will not work?????? Pin
Madhur Vade4-Jun-08 8:47
Madhur Vade4-Jun-08 8:47 
AnswerRe: SharePoint AJAX simply will not work?????? Pin
LionKing57724-Jun-08 19:35
LionKing57724-Jun-08 19:35 
Generalthis code does not work even the Walkthrough: Creating a Basic ASP.NET AJAX-enabled Web Part does not work either Pin
Member 399891924-Apr-08 0:43
Member 399891924-Apr-08 0:43 
None of these examples work

I tried my best but I still get the post back on my pages

Do you have a solution for this issue

Many thanks

Rodolfo
GeneralRe: this code does not work even the Walkthrough: Creating a Basic ASP.NET AJAX-enabled Web Part does not work either [modified] Pin
joebrockhaus12-May-08 9:02
joebrockhaus12-May-08 9:02 
GeneralRe: this code does not work even the Walkthrough: Creating a Basic ASP.NET AJAX-enabled Web Part does not work either Pin
Albert Lu12-May-08 10:49
Albert Lu12-May-08 10:49 
GeneralRe: this code does not work even the Walkthrough: Creating a Basic ASP.NET AJAX-enabled Web Part does not work either Pin
Member 399891912-May-08 21:37
Member 399891912-May-08 21:37 
GeneralRe: this code does not work even the Walkthrough: Creating a Basic ASP.NET AJAX-enabled Web Part does not work either Pin
Member 399891912-May-08 21:41
Member 399891912-May-08 21:41 
GeneralRe: this code does not work even the Walkthrough: Creating a Basic ASP.NET AJAX-enabled Web Part does not work either Pin
dave.dolan10-Sep-08 8:43
dave.dolan10-Sep-08 8:43 
GeneralRe: this code does not work even the Walkthrough: Creating a Basic ASP.NET AJAX-enabled Web Part does not work either Pin
vikpri4-May-09 7:32
vikpri4-May-09 7:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.