views:

201

answers:

4

I'm trying to use ResolveUrl() to set some paths in the code behind of a custom ASP.NET user control. The user control contains a navigation menu. I'm loading it on a page that's loading a master page.

When I call ResolveUrl("~") in my user control it returns "~" instead of the root of the site. When I call it in a page I get the root path as expected. I've stepped through with the debugger and confirmed, ResolveUrl("~") returns "~" in my user control code behind.

Is there some other way I should be calling the function in my user control code behind to get the root path of the site?

+1  A: 

look at System.Web.VirtualPathUtility.ToAbsolute.

Fredou
+1  A: 

Take a look at this MSDN example. http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl.aspx. and Rick Strahl's Web Log http://www.west-wind.com/Weblog/posts/154812.aspx

Wonde
I'm not sure how Rick Strahl's post applies. I am loading my control on a page. The Control.ResolveUrl is available but it isn't resolving.
WebJunk
A: 

I suppose if it works on the page but not the control i guess you could try

this.Page.ResolveUrl("~");
BritishDeveloper
This is probably part of the issue: the control Page property is empty at runtime.
WebJunk
Are you loading this control in using code behind?
BritishDeveloper
+2  A: 

wonde's comment above led me to the answer --

I was attempting to use ResolveUrl before the control's page load fired. Therefore there was no context page for the function yet.

I moved my code into the page load function and now it's resolving as expected.

Thanks for the nudge in the right direction.

WebJunk