Detecting Designmode in ASP.Net

来源:互联网 发布:毁坏的冬眠者数据库 编辑:程序博客网 时间:2024/06/05 01:00

Detecting Designmode in ASP.Net

Unlike WinForms, ASP.Net does not support a DesignMode property on the Page class, so there's no built in way to detect this. For building custom ASP.Net Server controls (not UserControls) this is crucial since ASP.Net actually renders control in the designer by calling the Render method.

The quick way to do this is:

if (HttpContext.Current == null)
   // You're in design mode

After some more playing around I also found that you can do this via the Site interface of the control as well:

if (this.Site.DesignMode)
   // You're in design mode

Unfortunately that doesn't work at runtime because this.Site doesn't exist. You can check for this.Site == null to detect runtime I suppose, but I suspect checking the current context is the most reliable. You can also do something like this to declare it:

bool DesignMode = (HttpContext.Current == null);

This doesn't work with Site because Site doesn't exist at construction. So HttpContext.Current is the better choice. If I need to check design mode I tend to add a private property to the control and just use the above as an assignment.

BTW, it's good to know that if you build Web Controls that they render on in the designer. If you have in the Render method that causes the control to fire and that control uses information about the current request it will fail which in turn causes the control to display as an error. That doesn't mean your control won't run, it just means there's an error that's happening at design time...

If you know your control doesn't render at Design time because it need Http Request information you can just immediately return.

To avoid the error you can force the control to immediately return from Render:

protected override Render(HtmlTextWriter)
{
 
  if (this.DesignMode)
      return;

   ...go on with processing
}

This results in the control not rendering anything and the designer displaying a simple text string that shows the control's name. As an alternate you can render some sort of static HTML placeholder.

This is what I actually ended up doing for the Tab Control I was working on because I've been having a hell of a time trying to set up a collection and have it filled at design time. The problem is that the form renders fine - but without any tabs because the collection is empty. So the first approach I had was simply to generate a simple two cell HTML table that looked like the tabs and uses some of the parameters (the Css class and sizing) so as you change properties these actually reflect in the designer.

However, in the end it turned out easier to just temporarily add a few tabs for the Render method and then delete them again at the end.

protected override void Render(HtmlTextWriter writer)
{
   if (this.DesignMode)
   {
      // *** Add a couple of tabs just for show
      this.AddTab("Tab 1","","Tab1");
      this.AddTab("Tab 2","","Tab2");
      this.SelectedTab = "Tab2";
   }

   // *** Render the actual control
   this.RenderControl();

   // *** Dump the output into the ASP out stream
   writer.Write(this.Output);
   base.Render (writer);

   if (this.DesignMode)
   {
      // *** Clear the tab pages we added they're
      this.TabPages.Clear();
   }

}

If I get designer support for the TabPage collection hooked I can drop out that Designmode code, but until then I at least now have a visual representation at design time...

 

posted on Monday, January 05, 2004 10:08 PM

原创粉丝点击