Saturday, September 25, 2010

How to create AJAX enabled web part in Sharepoint

When we talk about AJAX and sharepoint, there are several problems. Some people believe that if AJAX works with .net then there should not be any problem. But, there is a little problem however, not without a workaround.

The problem with Sharepoint and AJAX is mentioned in the below blog -
http://sharemypoint.wordpress.com/2007/11/11/webpart-and-ajax-second-postback-not-working-in-updatepanel/.

Make the below web.config entries in your web application -
http://zieglers.wordpress.com/2009/02/02/webconfig-entries-for-enabling-ajax-for-sharepoint-2007/

Windows SharePoint Services JavaScript has a “form onSubmit wrapper” which is used to override the default form action. This work is put in place to ensure that certain types of URLs, which may contain double byte characters, will fully work across most postback and asynchronous callback scenarios. The workaround involved registering a script at page startup.

I created an AJAX webpart with timer.

using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Eversheds.Connect.WebParts
{
[Guid("D1A860C1-7033-4052-AB47-DF979BA14728")]
public class WPContact : System.Web.UI.WebControls.WebParts.WebPart
{
Label label1;
Button button1;
UpdatePanel updatePanel1;
Timer tim;


protected override void OnInit(EventArgs e)
{
base.OnInit(e);
EnsureChildControls();
}

protected override void CreateChildControls()
{
base.CreateChildControls();
bool bDisabled = false;

try
{
//Fix for the UpdatePanel postback behaviour.
EnsurePanelFix();
EnsureUpdatePanelFixups();

updatePanel1 = new UpdatePanel();
updatePanel1.ID = "updatePanel1";
updatePanel1.ChildrenAsTriggers = true;
updatePanel1.UpdateMode = UpdatePanelUpdateMode
.Conditional;
//button1 = new Button();
//button1.ID = "btnClick";
//button1.Text = "Update";
//button1.Click += new EventHandler(button1_Click);
tim = new Timer();
tim.Interval = 2000;
tim.ID = "tim1";
tim.Tick += new EventHandler(tim_Tick);


label1 = new Label();
label1.ID = "lblShowTime";
label1.Text = string.Format("Updatedate:{0}", DateTime
.Now.ToLongTimeString());
updatePanel1.ContentTemplateContainer.Controls.Add(label1);

//updatePanel1.ContentTemplateContainer.Controls.Add(button1);
updatePanel1.ContentTemplateContainer.Controls.Add(tim);
AsyncPostBackTrigger trig = new AsyncPostBackTrigger();
trig.ControlID = tim.ID;
trig.EventName = "Tick";
updatePanel1.Triggers.Add(trig);

this.Controls.Add(updatePanel1);
//this.Controls.Add(label1);
//this.Controls.Add(button1);
}
catch (Exception ex)
{
throw ex;
}
}
void button1_Click(object sender, EventArgs e)
{
this.label1.Text = string.Format("Updatedat:{0}", DateTime
.Now.ToLongTimeString());
}

void tim_Tick(object sender, EventArgs e)
{
try
{
this.label1.Text = string.Format("Updatedat:{0}", DateTime
.Now.ToLongTimeString());
}
catch (Exception ex)
{
throw ex;
}
}

private void EnsurePanelFix()
{
//Fix problem with postbacks and form actions (DevDiv 55525)
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(WPContact
), "UpdatePanelFixup", fixupScript, true);
}
}

private void EnsureUpdatePanelFixups()
{
if (this.Page.Form != null)
{
string formOnSubmitAtt = this.Page.Form.Attributes[
"onsubmit"];
if (formOnSubmitAtt == "return _spFormOnSubmitWrapper();")
{
this.Page.Form.Attributes["onsubmit"] =
"_spFormOnSubmitWrapper();";
}
}
ScriptManager.RegisterStartupScript(this, typeof(WPContact),
"UpdatePanelFixup", "_spOriginalFormAction = document.forms[0].action;
_spSuppressFormOnSubmitWrapper=true;", true);
}
}
}

No comments:

Post a Comment