How to detect design time in a VS.NET 2003 control library project
-
02-07-2019 - |
Question
Code below is not working as expected to detect if it is in design mode (VS.Net 2003 - Control Library):
if (this.Site != null && this.Site.DesignMode == true)
{
// Design Mode
}
else
{
// Run-time
}
It is used in a complex user control, deriving from another user control and including other user controls on it.
Is there another way to detect design time in a VS.NET 2003 or what is the problem with the code above?
Solution
DesignMode
won't work from inside a constructor. Some alternatives (not sure if they work in 1.1) are
if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
or
call GetService(typeof(IDesignerHost))
and see if it returns something.
I've had better luck with the first option.
OTHER TIPS
I guess you could use HttpContext.Current == null as described at http://west-wind.com/weblog/posts/189.aspx
In .Net 2.0 there's Control.DesignMode (http://msdn.microsoft.com/en-us/library/system.web.ui.control.designmode.aspx). I guess you have a good reasons to stay on VS 2003 though, so upgrading might not be an option for you.
Update If you are doing Winforms, Component.DesignMode (http://msdn.microsoft.com/en-us/library/system.componentmodel.component.designmode.aspx) is the right way to check. Though, if this.Site.DesignMode doesn't work properly, Component.DesignMode might not work as well, as it does exactly the check you are doing (Site != null && Site.DesignMode).
This might be a long shot, but are you sure that your base control does not override the Site property?