Is it required to call ResolveUrl() before LoadControl()?

366 Views Asked by At

Is it required to call TemplateControl.ResolveUrl() before passing it to TemplateControl.LoadControl()?

Which way is preferred?

LoadControl(ResolveUrl("~/MyControl.ascx"));
LoadControl("~/MyControl.ascx");
LoadControl("MyControl.ascx");

or maybe ResolveClientUrl() ?

1

There are 1 best solutions below

0
On BEST ANSWER

It's not required. I wouldn't even recommend doing the first method, since it's just redundant and adds complexity. The tilde means it's relative to the application root already - ResolveUrl just changes it to be relative to the page or usercontrol you're calling it from. Either way, it will still be loaded.

The second way would be preferred as a best practice to help guard against relative structural changes to your project.

Even better would be to make the path string a resource or at least a constant to get rid of magic strings and avoid surprises altogether.