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?
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.
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
我想如果它能在页面上正常运行,但不能在控件上运行,我想您可以尝试
this.Page.ResolveUrl("~");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.