Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically Loading a UserControl with LoadControl Method (Type, object[])
    text
    copied!<p>I'm trying to return the html representation of a user/server control through a page method. It works when I call the overload which takes the virtual path to the user control, but not when I try to call the overload which takes a type. The sample code is below. Any suggestions?</p> <pre><code>[WebMethod] public static string LoadAlternates(string productId, string pnlId) { object[] parameters = new object[] { pnlId, productId }; return ControlAsString(typeof(PopupControl), parameters); } private static string ControlAsString(Type controlType, object[] parameters) { Page page = new Page(); UserControl controlToLoad; /* *calling load control with the type results in the *stringwriter returning an empty string */ controlToLoad = page.LoadControl(controlType, parameters) as UserControl; /* *However, calling LoadControl with the following overload *gives the correct result, ie the string rep. of the control. */ controlToLoad = page.LoadControl(@"~/RepeaterSamples/PopupControl.ascx") as UserControl; //some more code, then this... page.Controls.Add(controlToLoad); StringWriter sw = new StringWriter(); HttpContext.Current.Server.Execute(page, sw, false); return sw.ToString(); } </code></pre> <p>Any ideas why this StringWriter would return an empty string? I should point out that all the "page" lifecycle execute correctly irrespective of the method chosen to call LoadControl.</p> <p>Wanted to add - I <em>have</em> to use the <code>LoadControl(Type, object[])</code> overload. :-(</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload