Friday, March 12, 2010

Creating Ajax webparts

1)Download ASP.NET AJAX 1.0.

2)First of all you need to add

· System.Web
· System.Web.Extensions (v1.06)
· System.Web.Extensions.Design (v1.06)
. Syste.Data

3)Code:
using System;
using System.Web;

using System.Data;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace AjaxWebpart
{
[Guid("0eebf43e-a235-480b-a6df-54269c30892b")]
public class AjaxWebpart : System.Web.UI.WebControls.WebParts.WebPart
{
public AjaxWebpart()
{
this.ExportMode = WebPartExportMode.All;
}
private GridView grdSearch;
private TextBox txtSearch;
private Button btnSearch;
private Label lblMsg;
private UpdatePanel updatePanel;
private UpdateProgress updateProgress;
private static readonly object EventSubmitKey = new object();

//protected override void Render(HtmlTextWriter writer)
//{
// // TODO: add custom rendering code here.
// // writer.Write("Output HTML");
//}
[WebBrowsable(true), WebDisplayName("Image URL"), WebDescription("Image URL"), Personalizable(PersonalizationScope.Shared)]
public string LoadingImageURL
{
get
{
object _obj = ViewState["LoadingImageURL"];
if (_obj == null)
{
return string.Empty;
}
else
{
return (string)ViewState["LoadingImageURL"];
}

}
set { ViewState["LoadingImageURL"] = value; }
}
public event EventHandler Submit
{
add { Events.AddHandler(EventSubmitKey, value); }
remove { Events.RemoveHandler(EventSubmitKey, value); }
}
private void btnSearch_Click(object source, EventArgs e)
{

grdSearch.Visible = false;

System.Threading.Thread.Sleep(1500);

////Create DataTable Structure
DataTable Dtable = new DataTable("tmpTable");
////User_ID Col
Dtable.Columns.Add("User_ID", typeof(int));
Dtable.Columns["User_ID"].AutoIncrement = true;
Dtable.Columns["User_ID"].AutoIncrementSeed = 1;
////User_Name Col
Dtable.Columns.Add("User_Name", typeof(string));
////User_Department Col
Dtable.Columns.Add("User_Department", typeof(string));

DataRow dr = default(DataRow);

dr = Dtable.NewRow();
dr["User_Name"] = "Madhu Nomula";
dr["User_Department"] = "Sharepoint";
Dtable.Rows.Add(dr);

dr = Dtable.NewRow();
dr["User_Name"] = "Swathi Nomula";
dr["User_Department"] = "Technical";
Dtable.Rows.Add(dr);

dr = Dtable.NewRow();
dr["User_Name"] = "chinna Nomula";
dr["User_Department"] = "Production";
Dtable.Rows.Add(dr);

dr = Dtable.NewRow();
dr["User_Name"] = "Chinni Nomula";
dr["User_Department"] = "Technical";
Dtable.Rows.Add(dr);

string searchFor = "%";

if (txtSearch.Text != string.Empty)
{
searchFor = txtSearch.Text;
}

DataView dtView = new DataView(Dtable);
dtView.RowFilter = "User_Name LIKE '%'+'" + searchFor + "'+'%'";

if (dtView.Count > 0)
{
grdSearch.Visible = true;
grdSearch.AutoGenerateColumns = true;
grdSearch.DataSource = dtView;
grdSearch.DataBind();
lblMsg.Text = string.Empty;
}
else
{
lblMsg.Text = "Sorry, No Data Found!.";
grdSearch.Visible = false;
}


}



protected override void CreateChildControls()
{
Controls.Clear();

updatePanel = new UpdatePanel();
{
updatePanel.ID = "UpdatePanel1";
updatePanel.ChildrenAsTriggers = true;
updatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional;
}

updateProgress = new UpdateProgress();
updateProgress.ID = "UpdateProgress1";

string templateHTML = null;
if (LoadingImageURL == string.Empty)
{
templateHTML = "Loading ...";
}
else
{
templateHTML = "
'Loading...'
";
}

updateProgress.ProgressTemplate = new ProgressTemplate(templateHTML);

updateProgress.AssociatedUpdatePanelID = updatePanel.ClientID;

this.Controls.Add(updatePanel);

grdSearch = new GridView();
grdSearch.ID = "GrdSearch";

txtSearch = new TextBox();
txtSearch.ID = "txtSearch";

btnSearch = new Button();
btnSearch.Text = "Search";
btnSearch.ID = "BtnSearch";

lblMsg = new Label();
lblMsg.ID = "lblMsgError";

btnSearch.Click += btnSearch_Click;

updatePanel.ContentTemplateContainer.Controls.Add(txtSearch);
updatePanel.ContentTemplateContainer.Controls.Add(btnSearch);
updatePanel.ContentTemplateContainer.Controls.Add(grdSearch);
updatePanel.ContentTemplateContainer.Controls.Add(lblMsg);
updatePanel.ContentTemplateContainer.Controls.Add(updateProgress);

}


protected override void OnInit(EventArgs e)
{

base.OnInit(e);

//get the existing ScriptManager if it exists on the page
ScriptManager AjaxManager = ScriptManager.GetCurrent(this.Page);
if (AjaxManager == null)
{

//create new ScriptManager and EnablePartialRendering
AjaxManager = new ScriptManager();
AjaxManager.EnablePartialRendering = true;

//Fix problem with postbacks and form actions (DevDiv 55525)
Page.ClientScript.RegisterStartupScript(this.GetType(), this.ID, "_spOriginalFormAction = document.forms[0].action;", true);

//tag:"form" att:"onsubmit" val:"return _spFormOnSubmitWrapper()"
//blocks async postbacks after the first one
//not calling "_spFormOnSubmitWrapper()" breaks all postbacks
//returning true all the time, somewhat defeats the purpose of the
//_spFormOnSubmitWrapper() which is to block repetitive postbacks,
//but it allows MS AJAX Extensions to work properly

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();";
}
//add the ScriptManager as the first control in the Page.Form
this.Page.Form.Controls.AddAt(0, AjaxManager);

}
}
}

protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
updatePanel.RenderControl(writer);
}

//Class for Building progress tempales
public class ProgressTemplate : ITemplate
{
private string template;

public ProgressTemplate(string temp)
{
template = temp;
}

public void InstantiateIn(Control container)
{
LiteralControl ltr = new LiteralControl(this.template);
container.Controls.Add(ltr);
}


}
}

}


--------------

4)Now Extend the SharePoint web.config file, which is typically found in a directory with the following structure: drive\inetpub\wwwroot\VirtualDirectories\port number.

Modify the web.config as per the settings in this url

http://msdn.microsoft.com/en-us/library/bb861898.aspx

No comments:

Post a Comment