Workaround for ASP.NET 2.0 bug: LoadControl gives an exception in a TemplateControl
When you have your own custom TemplateControl and perform a LoadControl in there, this results in ax exception.
For instance, the following code will trigger the exception:
public class NavController : System.Web.UI.TemplateControl
{
public void LoadContent(string url)
{
Control ctrl = this.LoadControl(url);
this.Controls.Clear();
this.Controls.Add(ctrl);
}
}
The exception you get is:
[ArgumentNullException: Value cannot be null.
Parameter name: basepath]
System.Web.Util.UrlPath.Combine(String appPath, String basepath, String relative) +322
System.Web.UI.TemplateControl.LoadControl(String virtualPath) +29
SimpleContainer.NavController.LoadContent(String url)
The reason for this is that the property AppRelativeTemplateSourceDirectory is not correctly initialized. The workaround for this is to add the following code to your TemplateControl:
protected override void OnInit(EventArgs e)
{
this.AppRelativeTemplateSourceDirectory = Page.AppRelativeTemplateSourceDirectory;
base.OnInit(e);
}[Update: This bug no longer occurs in the RTM version]