blog community
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]

Posted 29-04-2005 9:19 by Raimondb
Filed under: ,

Comments

Anonymous wrote re: Workaround for ASP.NET 2.0 bug: LoadControl gives an exception in a TemplateControl
on 26-05-2005 19:49
is this the correct vb port:

Me.AppRelativeTemplateSourceDirectory = Page.AppRelativeTemplateSourceDirectory
Me.OnInit(e)

i just don't know ehere to put it.
Anonymous wrote re: Workaround for ASP.NET 2.0 bug: LoadControl gives an exception in a TemplateControl
on 27-05-2005 8:22
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
Anonymous wrote re: Workaround for ASP.NET 2.0 bug: LoadControl gives an exception in a TemplateControl
on 09-08-2005 16:07
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 ;-)
TrackBack wrote LoadControl() - Bug in ASP.NET 2.0!?
on 10-08-2005 12:21

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Enter code (required)
Powered by Community Server (Commercial Edition), by Telligent Systems