
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]
3 comments
is this the correct vb port:
Me.AppRelativeTemplateSourceDirectory = Page.AppRelativeTemplateSourceDirectory
Me.OnInit(e)
i just don’t know ehere to put it.
Will
This VB code should work AFAIK. You should put it in your own TemplateControl derived class, as an override of the OnInit function, as shown in the posting.
In the postings sample, it would be the OnInit function of the NavController class.
The VB syntax would be:
Protected Overloads OnInit(EventArgs e)
Begin
Me.AppRelativeTemplateSourceDirectory = Page.AppRelativeTemplateSourceDirectory
Me.OnInit(e)
End
Raimond
Hi Raimond,
I tried your workaround, but it doesn’t work. My class from where I call the LoadControl() method looks like this:
namespace myNSbla
{
public class RenderContent : System.Web.UI.Page
{
private void NoRecord()
{
this.AppRelativeTemplateSourceDirectory = base.AppRelativeTemplateSourceDirectory;
myobj tpl = (myobj)this.LoadControl("path");
tpl.ArticleId = Guid.Empty;
ContentPlaceholder.Controls.Add(tpl);
}
protected override void OnInit(EventArgs e)
{
this.AppRelativeTemplateSourceDirectory = Page.AppRelativeTemplateSourceDirectory;
base.OnInit(e);
}
}
}
This class is located in a class library project, and is called from codebehind from a .aspx. I’ve tried all I know, but nothing works 🙁
Greetings from Germany in hope of help 😉
Thomas