Knowing if a component is at Design time or Run time

I have a composite customer server control that requires access to the Page object

to render its children correctly.
protected override void CreateChildControls()
{
// add the wizard bars prepared in the constructor
AddWizardBars();
// parse web.config and load all wizard step controls
if (this.Page!=null) //do not enter here if we are working with the designer
{
LoadSteps();
if (!Page.IsPostBack) //initialize only first time control is loaded on page
MoveForward (null, InitialStep);
}
}
While this works correctly at run time when the control is in the context of a web page executing on the server it partially fails in the design mode
with "Error Creating Control" message (even though I check the Page property for null.
If in the Studio I switch between HTML and Design panes the component renders OK.
Commenting out parts of code that reference Page property makes the component render
correctly in the VS Designer.
I assume that the problem is due to the Page context not being available at the design time.
What is the best practice to determine withing the control if it is being executed in the
designer or runs on the server? As mentioned above checking Page for null does not seem
to be working perfectly.
Michal
0
morcinim
9/13/2004 5:51:52 PM
asp.net.hosting-open-forum 4216 articles. 0 followers. Follow

3 Replies
596 Views

Similar Articles

[PageSpeed] 50

You can use the Control class DesignMode property:

if( this.DesignMode )
//In design mode
else
//runtime mode
/Fredrik Normén - fredrikn @ twitter

Microsoft MVP, MCSD, MCAD, MCT

ASPInsiders
My Blog
0
Fredrik
9/13/2004 6:57:21 PM
Fredrik,

My component is a web custom server component. It extends the System.Web.UI.WebControl (derived from System.Web.UI.Control).
public class Wizard : WebControl, INamingContainer
{...
}
System.Web.UI.Control in turn only implements IComponent from
System.ComponentModel namespace so the property DesignMode is not available
within my class.
It seems that the method for checking the Design Mode is different for
form and web controls (why oh why) and at the moment I am interested
in the latter.
Michal
0
morcinim
9/14/2004 8:56:30 AM
The DesignMode is available in the Control class.

The Control class implements several interfaces:
public class Control : IComponent, IDisposable, IParserAccessor, IUrlResolutionService, IDataBindingsAccessor, IControlBuilderAccessor, IControlDesignerAccessor, IExpressionsAccessor
By the way, this forum is for ASP.Net 2.0 not ASP.Net 1.x, are you building a control for version 1.x? If so you are in the wrong thread.
This should work in ASP.Net 2.0:

using System;
using System.Web.UI.WebControls;
public class MyControl :WebControl
{
public MyControl()
{
}
protected override void CreateChildControls()
{
if( this.DesignMode )
//Design mode
else
//Not design mode
}
}


/Fredrik Normén - fredrikn @ twitter

Microsoft MVP, MCSD, MCAD, MCT

ASPInsiders
My Blog
0
Fredrik
9/14/2004 9:24:57 AM
Reply: