Code sample for a Asynchronous webpart

One of the requirements which I had encountered was to write a Asynchronous webpart which would show a progress bar on the webpart while the webpart is retrieving data from SQL in the backend.

Attaching Sample code of the webpart which I have created using Ajax similar to Business Data list which is available out of the box.

RaiseCallbackEvent method in the below sample is method which will contain all the business logic to fetch the details to be shown.

In the below sample I have added a System.Threading.Thread.Sleep(10000); to show the behavior of the webpart.

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

 

namespace AjaxWebPart
{
    [Guid("f93b9ec7-a679-41e4-a58b-202318e39bea")]
  
    public class AjaxWebPart : System.Web.UI.WebControls.WebParts.WebPart, ICallbackEventHandler
    {
        private string datadiv; //This will hold the name of your div tag
        private string ajaxdata; //This will hold the data that is returned via ajax…
     

        protected override void Render(HtmlTextWriter writer)
        {
            // TODO: add custom rendering code here.
                       this.datadiv = this.ClientID + "content";  //Uses the client side id of the web part instance + a name we give it
            writer.Write("<div id=\"" + this.datadiv + "\"><img src=\"_layouts/kpiprogressbar.GIF\" width\"150\"></div>");
          

        }
        protected override void OnLoad(EventArgs e)
        {

            this.datadiv = this.ClientID + "content";
            string js = Page.ClientScript.GetCallbackEventReference(this, "'blah'", "filldiv", "'" + this.datadiv + "'", true);
            string contentloader = "var ajaxcommands='';  window.onload = ajaxloader; function ajaxloader () { eval(ajaxcommands); } function filldiv(arg, ctx) { var mydiv = document.getElementById(ctx); mydiv.innerHTML = arg; }";
            if (Page.ClientScript.IsClientScriptBlockRegistered("contentloader ") == false)
                Page.ClientScript.RegisterClientScriptBlock(Page.ClientScript.GetType(), "contentloader", contentloader, true);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "myloader", " ajaxcommands = ajaxcommands + \"" + js + ";\";", true);
            base.OnLoad(e);

        }
        public string GetCallbackResult()
        {
            return this.ajaxdata;
        }
        public void RaiseCallbackEvent(string eventArgs)
        {
         
            StringBuilder sb = new StringBuilder();
            System.Threading.Thread.Sleep(10000);
            SPSite mySite = SPContext.Current.Site;
            SPWebCollection subSites = mySite.AllWebs;
            this.ajaxdata = mySite.Url.ToString() + "<BR>";
            for (int i = 0; i < subSites.Count; i++)
            {
                SPListCollection lists = subSites[i].Lists;

                for (int j = 0; j < lists.Count; j++)
                {
                    sb.Append (subSites[i].Title + "--" + lists[j].Title + "<BR>");
                    this.ajaxdata = sb.ToString();
                }
            }
        }

        

    }
}