Monday, 11 February 2008

AJAX/ASP.net in a Sharepoint Web Part

I've been playing around with AJAX and web parts. Here is a sample web part that uses AJAX. Simple two linkbuttons that if you click one, is says hello xxxxx (and xxxxx is what you put into the text box) and the second link says goodbye xxxxx. If you need any help modifying it, please let me know. Remember, you need the service pack installed for this to work.


using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Sample.SayHello
{
public class SayHelloWebPart : WebPart
{
private Label displayName;
private TextBox inputName;
protected override void CreateChildControls()
{
base.CreateChildControls();
//Fix for the UpdatePanel postback behaviour.
EnsurePanelFix();
LinkButton btnHello= new LinkButton();
LinkButton btnBye = new LinkButton();
UpdatePanel refreshName = new UpdatePanel();
ScriptManager scriptHandler = new ScriptManager();
displayName = new Label();
inputName = new TextBox();
//Set up control properties.
this.displayName.ID = "displayName";
this.displayName.Text = "Hello!";
this.inputName.ID = "inputName";
btnHello.ID = "btnhello";
btnJan.Text = " Hello ";
btnBye.ID = "btnBye";
btnBye.Text = " Goodbye";
scriptHandler.ID = "scriptHandler";
refreshName.ID = "refreshName";
refreshName.UpdateMode = UpdatePanelUpdateMode.Conditional;
refreshName.ChildrenAsTriggers = true;
//Add the EventHandler to the Button.
btnHello.Click += new EventHandler(ClickHelloHandler);
btnBye.Click += new EventHandler(ClickByeHandler);
//Add the user interface (UI) controls to the UpdatePanel.
refreshName.ContentTemplateContainer.Controls.Add(this.inputName);
refreshName.ContentTemplateContainer.Controls.Add(btnHello);
refreshName.ContentTemplateContainer.Controls.Add(btnBye);
refreshName.ContentTemplateContainer.Controls.Add(this.displayName);
//The ScriptManager control must be added first.
this.Controls.Add(scriptHandler);
this.Controls.Add(refreshName);
}
private void ClickHelloHandler(object sender, EventArgs args)
{
this.displayName.Text = "Hello "
+ this.inputName.Text.ToString() + ".";
}
private void ClickByeHandler(object sender, EventArgs args)
{
this.displayName.Text = "Goodbye "
+ this.inputName.Text.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(SayHelloWebPart), "UpdatePanelFixup",
fixupScript, true);
}
}
}
}

===========================================

Sorry, let out the web.config changes. This blog won't let me post up the changes, so email me and i will send it back.

1 comment:

Anonymous said...

Can you send me a sample of the web.config changes that need to take place.