简体   繁体   中英

ResolveURL not resolving in a user control

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.

我想如果它能在页面上正常运行,但不能在控件上运行,我想您可以尝试

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM