Detecting Designmode in ASP.Net

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值