Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

Adventures in Debugging an ASP.Net Control at design time


:P
On this page:

Today I had a hell of a time trying to debug a problem with an ASP.Net server control. The Control was working just fine at runtime, but I noticed that in the VS.Net  designer it always showed as an error button (red text name of the control rather than the rendered control). In fact, it was a specific interface I was creating and implementing on a set of controls and all controls with this interface were failing in the designer.

Figuring out problems of this sort are tricky at best because if your control performs any sort of sophisticated app or framework specific logic its quite likely that it won't properly Render in design time. It helps to understand that at DesignTime the designer instantiates the control, calls the constructor assigns any property values, fires OnInit and then fires Render() (there may be more but those are the ones I noticed - note that other event hooks like OnLoad and OnPreRender() don't fire at design time).

As you might expect this can be problematic as you essentially get a control that's firing only on half of its cylinders, right? <g>

One very helpful thing is to be able to detect desingmode which I wrote about last month.You basically can check HttpContext.Current being null to see whether you are in DesignMode. I tend to set a property called designmode in the constructor:

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

With that you can appropriately block code from firing that's application specific and requires objects that might not exist without an actual application running.

As mentioned the key things to watch for are:

  • OnInit Code
  • Render Code
  • Any Property values that fire code and get assigned from the designer

OnInit code is pretty straight forward - if there's anything non-self contained just bracket it out with the DesignMode property.

Render is a good one to provide your control with a decent design time display. If you have complex controls that have child controls or collections etc. it might be nice to actually have to control render all the child items etc. This works fine as long as the control is mostly self contained. However, I've found for many controls especially in a framework it's often a good idea to just render somesort of default display.

For example, I've been working on a generic translation switching control that ties into a framework. Because the translation code relies on a lot of different pieces of information that are based on the framework to render completely I opted for simply rendering out a string that displays a label and a single entry listbox.

protected override void Render(HtmlTextWriter writer)
{
   if (this
.DesignMode)
   {
      
// *** Just render an approximation of what it will look like
      
writer.Write("Please choose a language: <select style='width:100px'><option>English</option></select>"
);
    }

base.Render (writer);

The runtime code (which fires in OnLoad() and thus doesn't even fire at design time) on the other hand would create a label control (so it's localizable) and fills the list with values of all available languages. For UI positioning services however the above code is completely sufficient to dump the control.

Ok, all of this is somewhat logical, but here comes the part where I got hung up on yesterday for a couple of hours. If there is any code in the methods you are dealing with that call into components that cannot be loaded or accessed at designtime - even if the code doesn't run directly, the code will blow up.

What's happening is that I had some code that I knew wouldn't work at designtime in a Property Set. So I had the part that was a problem wrappered with the DesignMode flag to keep the code out of it. The control still would not render at designtime. Now if I went in and used a constant false value for the If statement it worked!

Well, it turns out that the Property Set was calling a Property Get of another property which was referencing another component in an external assembly that isn't necessarily available. The compiler can't load the other assembly, and voila our error occurs where the control cannot be loaded in the designer.

It's frustrating as hell to have a problem like this though because first off the problem is 4 or 5 levels removed from the actual control I was building. But most frustrating is the fact that there is no error message that gives you any sort of indication what's wrong. Remotely debugging a control in the designer is a hit or miss situation too - I've had it work on several occasions, but couldn't get any breakpoints to hit yesterday. Which makes sense in hindsight - the error was a compiler error so the control never bothered to even instantiate probably. But tell me that WHILE I'm trying to debug the thing <g>...

 


The Voices of Reason


 

Aaron
March 10, 2004

# re: Adventures in Debugging an ASP.Net Control at design time

Thanks for the tips, I'm having a similar problem in design time.

Kabindas
January 18, 2005

# re: Adventures in Debugging an ASP.Net Control at design time

Thanks men, you've just save my day with a very simple solution.

Peter Bromberg [MVP]
February 16, 2005

# re: Adventures in Debugging an ASP.Net Control at design time

Rick,
That was it! Thanks!

KMILO <camilo@cohete.net>
April 22, 2005

# HtmlControl as a control property

Hi men, I was reading ur article, and apparently you develop custom controls, I would like to make you a question how can I implement a control property as htmlcontrol, in other words, Im trying to receive in design time a page's control with a control property, it works well but when the page is on air the reflection methods could not resolve the control.

Any Ideas???

Thanks In Advance

new user
June 01, 2005

# re: Adventures in Debugging an ASP.Net Control at design time

i am having a problem in asp.net.
my asp.net page at design time changes at runtime. there are 2 divisions or frames which are next to each other on the page. in design mode it shows up fine but in runtime the frames overlap each other.

any suggestions?

shraddha_99_recj@yahoo.com
June 01, 2005

# re: Adventures in Debugging an ASP.Net Control at design time

sorry i forgot to mention my email - my email is shraddha_99_recj@yahoo.com.

Nigel Shaw
August 14, 2005

# re: Adventures in Debugging an ASP.Net Control at design time

Hi Rick,

At Exia we typically render the control in the Init or CreateChildControls event. For design time we use custom designers which render at design time with GetDesignTimeHtml. Seems to work fine, and we never have design/runtime conflicts or need to detect design/runtime.

Regards,

Nigel

Rick Strahl
August 14, 2005

# re: Adventures in Debugging an ASP.Net Control at design time

Hi Nigel,

At Init? Really? That doesn't seem like a good place to render output as sophisticated controls would likely have to handle other events following Init before they'd be ready to render their output. The GetDesignTimeHtml() call only works if you implement a custom designer too, which is great if you have or need one but kinda overkill if you don't.

Jim
August 19, 2005

# re: Adventures in Debugging an ASP.Net Control at design time

Thanks so much.

Just what I was looking for.

sapi
January 26, 2006

# re: Adventures in Debugging an ASP.Net Control at design time

You save me two times with this post!
Thanks a lot!

Cyber1000
September 12, 2006

# re: Adventures in Debugging an ASP.Net Control at design time

Wow, I was searching for the designmode property until I found out there is none.
But this short piece of code was exactly what I was searching for.
Thanks a lot!

Rick Strahl's Web Log
October 15, 2006

# Detecting Designmode in ASP.Net - Rick Strahl's Web Log

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.

Vladimir Kelman
April 21, 2008

# re: Adventures in Debugging an ASP.Net Control at design time

Hi Rick! Sorry for unrelated question. I'm trying to add a property to a custom BasePage : System.Web.UI.Page in such a way, that it will be visible in Visual Studio at design time. I'd like to be able to set it at design time, so it will persist. (Details here:
http://pro-thoughts.blogspot.com/2008/04/design-time-support-for-custom.html)

I would highly appreciate any ideas on doing that.

Jeff
July 30, 2008

# re: Adventures in Debugging an ASP.Net Control at design time

Hi Rick. When you were talking about OnInit and Render running in design mode, is that only the OnInit and Render on the control or do they also run for the Page? Just wondering if there are times that we need to check if we're in Design mode on actual pages if we access context information.

Danya
September 09, 2008

# re: Adventures in Debugging an ASP.Net Control at design time

I have an issues in creating controls at design time and at runtime. I need to render control specified by runtime and followed by controls created at design time. But it always render the desing time control and underneath the runtime controls are created. I am using master page as well.

I place the code [for runtime controls] in the page load event. I am not sure how to do it. Any help on this is highly appreciated.

thanks,
Danya

Priya
June 24, 2009

# re: Adventures in Debugging an ASP.Net Control at design time

HI,

I am rendering copositecustomcontrol on web page. when i set any property of control in design mode, it is rendering HTML in source.... how can I avoide this? It should add proeprty and value as attribute to control tag.

Please suggest.

Moiz Tankiwala
February 27, 2010

# re: Adventures in Debugging an ASP.Net Control at design time

I have a very weird problem. I have customized the GridView control which renders correctly for a while. Then when I make changes to it, like edit templates or change any property, the design mode errors out and says that cannot set the value on a particular property.

Is there some means to debug the code that is executed in design time?

This problem is driving me crazy!!

Thanks for looking this up,

-Moiz Tankiwala

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024