Ajax Webpart displays Webservice data

Ajax Webpart displays Webservice data

In this post I want to show how to create an Ajax Webpart, which receives its data from a Webservice – and until the data arrived – shows a status bar.

As base for the Webpart, I took the one from Mark Collins and his great article http://sharethispoint.com/archive/2006/11/15/Build-web-parts-with-ajax.aspx.

The approach is to render the Webpart with only a <DIV>, and let the client – after it finishes querying the Webservice – fill the data into the <DIV> previously created.

The original version from Mark Collins comes without server side controls. I created the <DIV> as a HTMLGenericControl, because I wanted to have more control over it (and I was curious if it worked).

The basic stays the same. Long running operations are moved from your normal Webpart to the RaiseCallbackEvent method.

public
string GetCallbackResult()

{


return
this._AjaxData;

}

 

public
void RaiseCallbackEvent(string eventArgument)

{


this._AjaxData = CalculateContent();

}

The content returned by the CalculateContent Method is a simple string, which the clients renders later into the <DIV> element.

string CalculateContent()

{


string returnString = string.Empty;

 


try

{


TextWriter tw = new
StringWriter();


HtmlTextWriter _TxtWriter = new
HtmlTextWriter(tw);

 

_Grid = new
SPGridView();

_Grid.DataSource = GetGridDataSource();

 


// catch if the grid datasource is empty


if (_DS == null)

{


return
"<IMG src=\"/_layouts/images/BROKENLINK.GIF\" border=\"0\"/> no data received";

}


_Grid.DataBind();

 

_Grid.RenderControl(_TxtWriter);

returnString = _TxtWriter.InnerWriter.ToString();

}


catch (Exception ex)

{

_ErrorText += "Error in CalculateContent:\r\n:" + ex.ToString() + "\r\n";

returnString = _ErrorText;

}

 


return returnString;

}

This approach has a big disadvantage. You cannot use sorting, paging and filtering in the SPGridView, because it is created on the client side. You can use grouping, though. So what can we do about sorting and paging?

  • Sorting can be realized with a Webpart property, which takes the sort field an order, and sorts the underlying DataSet/DataView.

private
if (!String.IsNullOrEmpty(_SortString))

{


string[] sortArray = _SortString.Split(‘;’);


if (sortArray.Length == 2)

{

SortExpression = sortArray[0];

SortDirection = sortArray[1];


if (DataSetContainsField(_DS, SortExpression))

{

_DS.Tables[0].DefaultView.Sort = SortExpression + " " + SortDirection;

}

}

}

  • Filtering is also realized with a Webpart property, and modifying the DataSet/DataView

if (!String.IsNullOrEmpty(_FilterString) && _FilterString.Contains("="))

{


string filterColumnName = _FilterString.Substring(0, _FilterString.IndexOf(‘=’));

 


// catch typos in the filter


if (DataSetContainsField(_DS, filterColumnName))

{

_DS.Tables[0].DefaultView.RowFilter = _FilterString;

}

}

  • Paging: As soon, as I find a better solution, I will tell you about it J

Download the code here.

1 Comment

Erik Burger

Reply

Hi,

I know it’s been a while since you wrote this post but perhaps you are interested in the solution I came up with for filtering (including multi-column), sorting and paging the SPGridView.

I wrote a series of articles on the subject here: http://www.reversealchemy.net/tag/spgridview/

If you have the time (and interest) to take a look, I’d love to hear your comments.

Kind regards,

Erik

 

Leave a Reply