Friday, April 6, 2007

Implementing ASP.Net AJAX in MOSS 2007

Description

To implement ASP.Net AJAX in MOSS 2007 environment.

Technology Used

Microsoft ASP. NET AJAX, MOSS 2007

Steps involved in implementing AJAX in MOSS 2007

1. Install ASP.NET AJAX on servers in your farm

2. Download the full "ASP.NET 2.0 AJAX Extensions 1.0" from
ajax.asp.net.

3. Extend your SharePoint site’s web.config file with Microsoft ASP.NET AJAX 1.0. Modify your SharePoint web.config file with the appropriate settings for the ASP.net Ajax Extensions. Mike Ammerlaan has the details on this.

4. No need to add the ScriptManager into a SharePoint MasterPage

5.Create a SharePoint Web Part with controls that implements AJAX

a. Create a Web Part Project

b. Add references to Microsoft.SharePoint, System.Web dlls.

c. Implement the following code.

This code creates a Calendar, Textbox and a Button control in an UpdatePanel. When the Calendar control is clicked the selected date is populated in the Textbox Control. When the “Clear Text” button control is clicked the text in the Textbox control is cleared.
d. Points to be noted
i. Override the OnInit method.
ii. The EnsureUpdatePanelFixups method is used to register the Script Manger control in the web part.

using System;
using System.Collections.Generic;
using System.Text;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;

namespace CalenderWP
{
public class Calender : System.Web.UI.WebControls.WebParts.WebPart
{
UpdatePanel up;
Calendar calender;
TextBox text;
Button button;
private ScriptManager _AjaxManager;

protected override void RenderContents(HtmlTextWriter writer)
{

up.RenderControl(writer);
writer.Write("
");
}

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

protected override void CreateChildControls()
{
up = new UpdatePanel();
up.ID = "UpdatePanel1";
up.ChildrenAsTriggers = true;
up.UpdateMode = UpdatePanelUpdateMode.Conditional;
this.Controls.Add(up);

this.calender = new Calendar();
this.calender.ID = "Calender";
this.calender.SelectionChanged += new EventHandler(calender_SelectionChanged);
up.ContentTemplateContainer.Controls.Add(this.calender);

this.text = new TextBox();
this.text.Text = DateTime.Now.ToShortDateString();
up.ContentTemplateContainer.Controls.Add(this.text);

this.button = new Button();
this.button.ID = "button";
this.button.Text = "Clear Text";
this.button.Click += new EventHandler(button_Click);
up.ContentTemplateContainer.Controls.Add(this.button);

}

void calender_SelectionChanged(object sender, EventArgs e)
{

this.text.Text = calender.SelectedDate.ToShortDateString();
}

void button_Click(object sender, EventArgs e)
{
this.text.Text = string.Empty;
}

private void EnsureUpdatePanelFixups()
{

_AjaxManager = ScriptManager.GetCurrent(this.Page);

if (_AjaxManager == null)
{
_AjaxManager = new ScriptManager();
_AjaxManager.EnablePartialRendering = true;

Page.ClientScript.RegisterStartupScript(typeof(Calender), this.ID, "_spOriginalFormAction = document.forms[0].action;", true);

if (this.Page.Form != null)
{
string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];
if (!string.IsNullOrEmpty(formOnSubmitAtt) && formOnSubmitAtt == "return _spFormOnSubmitWrapper();")
{
this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";
}

this.Page.Form.Controls.AddAt(0, _AjaxManager);
}
}
}



#region Properties
///


/// Exposes the Page's script manager. The value is not set until after OnInit
///

[WebPartStorage(Storage.None)]
public ScriptManager AjaxManager
{
get { return _AjaxManager; }
set { _AjaxManager = value; }
}
#endregion
}

Friday, April 6, 2007

Implementing ASP.Net AJAX in MOSS 2007

Description

To implement ASP.Net AJAX in MOSS 2007 environment.

Technology Used

Microsoft ASP. NET AJAX, MOSS 2007

Steps involved in implementing AJAX in MOSS 2007

1. Install ASP.NET AJAX on servers in your farm

2. Download the full "ASP.NET 2.0 AJAX Extensions 1.0" from
ajax.asp.net.

3. Extend your SharePoint site’s web.config file with Microsoft ASP.NET AJAX 1.0. Modify your SharePoint web.config file with the appropriate settings for the ASP.net Ajax Extensions. Mike Ammerlaan has the details on this.

4. No need to add the ScriptManager into a SharePoint MasterPage

5.Create a SharePoint Web Part with controls that implements AJAX

a. Create a Web Part Project

b. Add references to Microsoft.SharePoint, System.Web dlls.

c. Implement the following code.

This code creates a Calendar, Textbox and a Button control in an UpdatePanel. When the Calendar control is clicked the selected date is populated in the Textbox Control. When the “Clear Text” button control is clicked the text in the Textbox control is cleared.
d. Points to be noted
i. Override the OnInit method.
ii. The EnsureUpdatePanelFixups method is used to register the Script Manger control in the web part.

using System;
using System.Collections.Generic;
using System.Text;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;

namespace CalenderWP
{
public class Calender : System.Web.UI.WebControls.WebParts.WebPart
{
UpdatePanel up;
Calendar calender;
TextBox text;
Button button;
private ScriptManager _AjaxManager;

protected override void RenderContents(HtmlTextWriter writer)
{

up.RenderControl(writer);
writer.Write("
");
}

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

protected override void CreateChildControls()
{
up = new UpdatePanel();
up.ID = "UpdatePanel1";
up.ChildrenAsTriggers = true;
up.UpdateMode = UpdatePanelUpdateMode.Conditional;
this.Controls.Add(up);

this.calender = new Calendar();
this.calender.ID = "Calender";
this.calender.SelectionChanged += new EventHandler(calender_SelectionChanged);
up.ContentTemplateContainer.Controls.Add(this.calender);

this.text = new TextBox();
this.text.Text = DateTime.Now.ToShortDateString();
up.ContentTemplateContainer.Controls.Add(this.text);

this.button = new Button();
this.button.ID = "button";
this.button.Text = "Clear Text";
this.button.Click += new EventHandler(button_Click);
up.ContentTemplateContainer.Controls.Add(this.button);

}

void calender_SelectionChanged(object sender, EventArgs e)
{

this.text.Text = calender.SelectedDate.ToShortDateString();
}

void button_Click(object sender, EventArgs e)
{
this.text.Text = string.Empty;
}

private void EnsureUpdatePanelFixups()
{

_AjaxManager = ScriptManager.GetCurrent(this.Page);

if (_AjaxManager == null)
{
_AjaxManager = new ScriptManager();
_AjaxManager.EnablePartialRendering = true;

Page.ClientScript.RegisterStartupScript(typeof(Calender), this.ID, "_spOriginalFormAction = document.forms[0].action;", true);

if (this.Page.Form != null)
{
string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];
if (!string.IsNullOrEmpty(formOnSubmitAtt) && formOnSubmitAtt == "return _spFormOnSubmitWrapper();")
{
this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";
}

this.Page.Form.Controls.AddAt(0, _AjaxManager);
}
}
}



#region Properties
///


/// Exposes the Page's script manager. The value is not set until after OnInit
///

[WebPartStorage(Storage.None)]
public ScriptManager AjaxManager
{
get { return _AjaxManager; }
set { _AjaxManager = value; }
}
#endregion
}