views:

535

answers:

2

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?

A: 

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?

Franci Penov
For HttpContext; it is winform application, not web. For staying on VS 2003; for now it is not an option unfortunatelly.
rovsen
Component.DesignMode supported in: 3.5, 3.0, 2.0. The software is running on Framwrork 1.1. Thanks for your interest.
rovsen
According to the MSDN page of Component.designMode, that property has existed since 1.0.
Franci Penov
+4  A: 

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.

Nicholas Piasecki
if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)worked. thanks..
rovsen

related questions