tags:

views:

1164

answers:

3

Im trying to find a best practice to load usercontrols using Ajax.

My first approach where simply using an UpdatePanel and popuplating it with LoadControl() on ajax postbacks but this would rerender other loaded usercontrols in the same UpdatePanel. Also I cannot have a predefined set of UpdatePanels since the number of UserControls I need to load will vary.

Is there any best practice for this type of scenario?

If needed I could implement a framework or some type of custom controls if that would be a solution but I would love to do this with ASP.NET 3.5 and the AjaxControlToolkit if possible.

+1  A: 

I'm not sure, but maybe this tutorial by Scott Guthrie could be useful.

splattne
Great link - thanks. I have always wanted to know how to do this, but never really had a good reason to spend the time trying. If I need to in the future, now I have some help.
Jason Bunting
+3  A: 

Probably dozens of high-brow reasons for not doing it this way, but simply initalize a page, add the usercontrol, then execute and dump the resulting html wherever it may behoove you, is (in my simpleminded view) so mind-numbingly fast&fun, that I just have to mention it...

Skip the UpdatePanels, just use a Label, a plain old span, or how about an acronym...

Using JQuery on the client side :
$('#SomeContainer').Load("default.aspx?What=GimmeSomeSweetAjax");

ServerSide:
if(Request.QueryString["What"]==GimmeSomeSweetAjax)
{

   Page page = new Page();  
   Control control = (Control)LoadControl("~/.../someUC.ascx");  
   StringWriter sw = new StringWriter();  
   page.Controls.Add(control);  
   Server.Execute(page, sw, false);  
   Response.Write(sw.ToString());  
   Response.Flush();  
   Response.Close();

}

Nothing else executes, and the page's lifecycle has a real Kevorkian moment ;-)

Morten Bergfall
A very basic solution but it is working great, thanks! :-)
A: 

you have to know that the 'Server.Execute' approach and inserting it via InnerHtml is NOT working for usercontrols with ajax controls - the script tags are NOT execute on the DOM when inserted via InnerHtml. FIY. tried it myselsf and moved back to updatePanels...

zahi.

zahi