Navigate Up

SharePoint Ajax web part and Timer problem   

Tags: Moss 2007, Wss 3.0
Technorati Tags: ,

The last days I have been in trouble with Ajax and a web part which had to fire after a time.

This should been piece of cake since I did some earlier Ajax web parts.

It turned out that it was not that easy at all.

First of all when you start with Ajax and SharePoint download the Ajax extensions from this page

After installing them you are not ready you need to edit the web.config add some scripts a good site for the extensions is the one of Tobias Zimmergren

After this I started my development well it looked pretty good:

/// <summary>

/// This web part uses an SPGridView inside of an updatepanel to display a dataset. The update panel automatically handles

/// all the ajaxy stuff for post backs with paging and sorting.

/// </summary>

[XmlRoot(Namespace = "SpaarneZiekenhuis.OCS.WebParts")]

[Guid("41B3B790-7578-4cec-9F31-F342FD2B1192")]

public class OcsQueue : System.Web.UI.WebControls.WebParts.WebPart

{

Timer timer = new Timer();

Label label1 = new Label();

private Label displayQueue;

private TextBox inputName;

[DefaultValue(""), WebBrowsable(true), Category("ProgressTemplate"), Personalizable(PersonalizationScope.Shared)]

public string ImagePath { get; set; }

[DefaultValue(""), WebBrowsable(true), Category("ProgressTemplate"), Personalizable(PersonalizationScope.Shared)]

public string DisplayText { get; set; }

 

protected override void CreateChildControls()

{

try

{

base.CreateChildControls();

 

//Fix for the UpdatePanel postback behaviour.

EnsurePanelFix();

timer = new Timer();

UpdatePanel refreshName = new UpdatePanel();

displayQueue = new Label();

 

//Set up control properties.

this.displayQueue.ID = "displayQueue";

this.displayQueue.Text = "Waiting in Queue";

refreshName.ID = "refreshName";

refreshName.UpdateMode = UpdatePanelUpdateMode.Conditional;

refreshName.ChildrenAsTriggers = true;

this.timer.ID = "TimerOcs";

this.timer.Interval = 10000;

this.timer.Tick += new EventHandler<EventArgs>(ClickHandler);

//Add the EventHandler to the Button.

 

//Add the user interface (UI) controls to the UpdatePanel.

refreshName.ContentTemplateContainer.Controls.Add(this.timer);

refreshName.ContentTemplateContainer.Controls.Add(this.displayQueue);

 

 

//The ScriptManager control must be added first.

if (ScriptManager.GetCurrent(this.Page) == null)

{

ScriptManager scriptHandler = new ScriptManager();

scriptHandler.ID = "scriptHandler";

scriptHandler.EnablePartialRendering = true;

this.Controls.Add(scriptHandler);

}

this.Controls.Add(refreshName);

}

catch (Exception ex)

{

Console.WriteLine(ex.ToString());

}

}

 

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(OcsQueue), "UpdatePanelFixup",

fixupScript, true);

}

}

 

private void ClickHandler(object sender, EventArgs args)

{

this.displayQueue.Text = string.Format("Waiting in Queue {0}", GetWaitingQueue());

 

}

 

We'll all looks good be sure to add the script manager before you add the update panel you can find the base of this script here

The script worked like a charm then I started to edit it because I needed a timer I need to do a update ever 10 sec or so to see if my queue has been changed.

As soon as I added the timer control and tested it I got a web part error I could not find it what it was after some debugging.

This did not give any errors I found out that when I stepped over the timer control the web part was showing correctly.

Mm that was a strange one I tried to Bing/Google around timer and Ajax after a while I found that some people added the

<asp:ScriptManager runat="server" ID="ScriptManager1"></asp:ScriptManager> Tag after trying to add the tag I found out my Ajax

Enabled webpart was working.

So after doing little tests I can tell the following

Because the timer does auto postback it is possible that the scriptmanager is not loaded at time of load when you add it from code.

This is also the reason that sometimes my webpart was showing correctly and sometimes not.

Till now it has been the timer control is the only control where I needed to at the script manager in the masterpage instead of doing it by code.

As fallback scenario I also coded in code to be sure there is a script manager.

 

I hope it helps people that are struggling with the same issue.

If you have a different solution please post a comment.

 

 
Posted by  System Account  on  10/2/2009
0  Comments  |  Trackback Url  | 0  Links to this post | Bookmark this post with:                        
 

Links to this post

Comments

Name *:
URL:
Email:
Comment:


CAPTCHA Image Validation




© Copyright 2009 KbWorks