blog community

Welcome to blog community Sign in | Join | Help
in Search

Raimond Brookman

Architecture and .NET Development

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]
Published Friday, April 29, 2005 9:19 AM by Raimondb
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Will said:

is this the correct vb port:

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

i just don't know ehere to put it.
May 26, 2005 7:49 PM
 

Raimond said:

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
May 27, 2005 8:22 AM
 

Thomas said:

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 ;-)
August 9, 2005 4:07 PM
 

TrackBack said:

August 10, 2005 12:21 PM

Leave a Comment

(required) 
(optional)
(required) 
Submit

This Blog

Syndication

News

Subscribe in NewsGator Online

Subscribe in Bloglines

Add to Google


 Visitors since June 2005




Powered by Community Server, by Telligent Systems