If you’re working with User Controls (or other server side controls) and your file structure looks something like this:
[] = directory
[root]
[userControls]
ucTest.ascx
.....
ucContainer.aspx
Web.config
And in your ucContainer.aspx file you have something similar to this:
< %@ Register Src="userControls/ucTest.ascx" TagName="UserControl" TagPrefix="uc1" %>
...
...
<uc1:UserControl id="ucUserControl" runat="server" />
...
...
And from your code behind, you are loading the control like this:
ucUserControl.LoadControl("userControls/ucTest.ascx");
It will compile with no problems, BUT at run time it will give you this nasty error message:
Exception Details: System.Web.HttpException: The file '/path/to/ucTest.ascx' does not exist.
This happens because when you load your control, you need to do it like this:
ucUserControl.LoadControl("~/userControls/userControl.ascx");
Also, make sure you add the tilde (~) character in your .aspx Register directive, Like so:
< %@ Register Src="~/userControls/ucTest.ascx" TagName="UserControl" TagPrefix="uc1" %>
The ~ (tilde) character is the root path reference syntax which lets you add a reference to your controls, pages, etc. without having to hard code relative paths into your URLs like so:
ucUserControl.LoadControl("../../userControls/userControl.ascx");
Using this syntax will let you move your controls to other sub directories (if you ever need to) without having to worry about going back to your ucContainer.aspx.sc file and change the ‘hard coded’ path to the UC’s new location.
Of course, if the .ascx file and its container reside in the same directory, you don’t need to include the tilde (~) character into your URL’s path.
Hope this helps