OK, I have found a solution to this. I will try to explain clearly.
The problem was that databinding the Ajax Data Control GridView is done like this:
<script type="text/javascript">
function pageLoad(sender, e) {
Cuyahoga.Modules.CatMod.Web.MyWebService.Test_Method(onLoadSuccess);
//alert('pageLoad fired.');
}
function PopulateADCGridView1(result) {
ADCGridView1.set_dataSource(result);
ADCGridView1.dataBind();
alert('onLoadSuccess PopulateADCGridView1 Static fired');
}
</script>
The databinding is by reference to the 'ADCGridView1' explicitly. Therefore the controls are named identically when multiple instances of the control are used. Boom! Not work.
So you have to dynamically create the control and add it to the update panel. You then use the controls ID and ClientID to generate the javascript to populate it via the web service of your choice.
I generated the ID of the control by using a random string generator. This was just to make it easy. There will be a better way (like adding the property in the page parkup, but the CMS I use does not allow this) The code for this is here:
private string RandomString(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
Create a property for the Ajax Data Controls ID to be stored in. This will store the Ajax Data Controls GridView ID from the current user control instance.
//GridView ID to be set for each instance
private String _ADCGridViewID;
public String ADCGridViewID {
get{ return _ADCGridViewID; }
set{_ADCGridViewID = value; }
}
Then inside the PageLoad of the control code create the control and add them to the Update Panel:
protected void Page_Load(object sender, EventArgs e)
{
this.ADCGridViewID = "ADCGV_"+RandomString(8,true); //String variable
AjaxDataControls.GridView dGV = new AjaxDataControls.GridView();
dGV.ID = ADCGridViewID;
dGV.EnableViewState = true;
UpdatePanel uDP = this.FindControl("UpdatePanel1") as UpdatePanel;
uDP.ContentTemplateContainer.Controls.Add(dGV);
}
Then also inside the PageLoad, create the ClientScriptManager to add the javascript required to populate your control and also register 'add_load' event scripts to make them work on load (this was the tricky part).
//Register JavaScript via RegisterStartupScript
ClientScriptManager csm = Page.ClientScript;
csm.RegisterStartupScript(this.GetType(), "pageLoad"+ ADCGridViewID,
@"<script type='text/javascript'>" + System.Environment.NewLine +
@"function onLoad"+ADCGridViewID+"(result) {" + System.Environment.NewLine +
ADCGridViewID + @".set_dataSource(result);" + System.Environment.NewLine +
ADCGridViewID + @".dataBind();" + System.Environment.NewLine +
//@"alert('onLoad " + ADCGridViewID + " fired');" + System.Environment.NewLine +
@"}" + System.Environment.NewLine +
//
@"function populate"+ADCGridViewID+"() {" + System.Environment.NewLine +
@" Cuyahoga.Modules.CatMod.Web.MyWebService.Test_Method(onLoad"+ADCGridViewID+");" + System.Environment.NewLine +
//@" alert('function populate" + ADCGridViewID + " fired.');" + System.Environment.NewLine +
@"}" + System.Environment.NewLine +
//
@"function populate() {" + System.Environment.NewLine +
@" Cuyahoga.Modules.CatMod.Web.MyWebService.Test_Method(PopulateADCGridView1);" + System.Environment.NewLine +
@"}" + System.Environment.NewLine +
//
@"Sys.Application.add_load(populate" + ADCGridViewID + ");" + System.Environment.NewLine +
@"</script>" + System.Environment.NewLine + System.Environment.NewLine);
This will generate the call to the web service and populate the control based upon the ID and ClientID of the dynamically created Ajax Data Control.
I have added 5 instances of the control to a page and they all populate :)