LoadControl VirtualPath question

G

George Durzi

I have a C# in my web application called BasePage. It inherits from
System.Web.UI.Page. I'm using page inheritance in my web app so all my pages
have an identical look and feel by inheriting from BasePage.

In my BasePage class, I have an OnInit method defined as shown:

protected override void OnInit(System.EventArgs e)
{
this.Controls.AddAt(0, LoadControl("_header.ascx"));
base.OnInit(e);
}

_header.ascx, and _footer.ascx are in the root of the virtual directory. And
if I had a page called default.aspx which inherited from BasePage,
everything is cool.

Let's say however, that I have a directory called misc, and I have a page
called misc.aspx within that directory that inherits BasePage. When I browse
it, I get an error.

The error occurs at this.Controls.AddAt(0, LoadControl("_header.ascx"));
because the app obviously can't find _header.ascx at
c:\inetpub\wwwroot\LRWIntranet\misc\_header.ascx

But if I change the OnInit function in BasePage trying to have it always
know to look for _header.ascx in the root, my app breaks.
For example:
this.Controls.AddAt(0, LoadControl("/_header.ascx"));
I get The virtual path '/_header.ascx' maps to another application, which is
not allowed. when I browse to http://localhost/lrwintranet/misc/misc/aspx

I always want the call to LoadControl to look in the root of the virtual
directory, but I can't get it to do that, any idea?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top