Thursday, November 5, 2009

Ajax webpart class inheriting the Webpart class

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;

namespace AuctionsWP
{
public class clsLoadAuctionsWP:WebPart
{
#region "Global variable declarations"

protected Control _control = null;
protected string _exception = String.Empty;

//This UserControls is a folder in the VirtualServer
//directory of your Sharepoint WebApplication.
//We have to create this folder in the VirtualServer
//directory and copy the UserControl at that location
protected string UserControlPath = @"~/UserControls/";

protected string UserControlFileName = @"UCAuctions.ascx";

#endregion

#region "Create Child Controls method to create the controls for the webpart"

protected override void CreateChildControls()
{
this.FixFormAction();

try
{
_control = this.Page.LoadControl(UserControlPath + UserControlFileName);
this.Controls.Add(_control);

}
catch (Exception ex)
{
_exception += "CREATE ERROR:" + ex.Message;
}
finally
{
base.CreateChildControls();
}
}
#endregion

#region "RenderControls method , to render the controls inside the webpart class"

protected override void RenderContents(HtmlTextWriter writer)
{
try
{
base.RenderContents(writer);
}
catch (Exception ex)
{
_exception += "RENDER ERROR:" + ex.Message;
}
}

#endregion

#region "Fix form Action method"
private void FixFormAction()
{
//STaging Script
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(clsLoadAuctionsWP), "UpdatePanelFixup", "_spOriginalFormAction = document.forms[0].action;_spSuppressFormOnSubmitWrapper=true;", true);


//// Production Script
//if (this.Page.Form != null)
//{
// String fixupScript = @"_spBodyOnLoadFunctionNames.push(""_initFormActionAjax""); ";
// ScriptManager.RegisterStartupScript(this, typeof(clsLoadAuctionsWP), "UpdatePanelFixup", fixupScript, true);
//}

}
#endregion
}
}

No comments:

Post a Comment