Scott Hanselman

Am I running in DesignMode?

October 1, '03 Comments [5] Posted in ASP.NET
Sponsored By

Well, crap.  It's 10:10pm.  I'm at work.  I missed the Portland [.NET] Bloggers Dinner.  It was one of those coding things...just one more class...then I'll get validation working...then I'll add configurability...you know.  Poop.

But, I did learn something interesting that wasn't obvious to yours truly.  I have a class heirarchy for my ASP.NET pages so the developers that use my infrastructure to build bankings sites have a few things always available to them.  The pages derive from blah.blah.blah.SharedBasePage (ya, I know it's a lame name) which derives from LocalizedPage.  It happens to be a heritage that spans 3 related assemblies.  They all used to be in the same Web Project, but I started pushing them up in my recent mad refactoring. 

Now, at SOME point along the line, I lost Forms Design Time support in VS.NET.  I started getting this doozy: "The designer could not be shown for this file because none of the classes within it can be designed."

This perplexed me, but really, I don't care since I'm all about HTML and asp:this and asp:that.  But, my users/developers will care, so it was eating at me.  The problem was, I couldn't remember when I broke it.

Yada yada yada, it turns out I had a line like this in a constructor up the chain:

resmgr = ((System.Resources.ResourceManager)HttpContext.Current.Application[Constants.RESOURCESTRINGS]);

And when VS.NET switches into Design Mode, it actually INSTANTIATES the page class.  (Read that again if you didn't get it, I didn't.) 

So, of course at Design Time, the HttpContext.Current is null.  So, that line now looks like this:

if (HttpContext.Current != null)
   
resmgr = ((System.Resources.ResourceManager)HttpContext.Current.Application[Constants.RESOURCESTRINGS]);
else
      System.Diagnostics.Trace.WriteLine("Note: We are NOT running with valid HttpContext. We are probably in the IDE.  Or, something is wrong with the flux capacitor.");

So, magically, all my Design Time support is back, and I'm actually using it.  Maybe I wasn't such an HTML bad-ass after all.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Wednesday, 01 October 2003 07:33:01 UTC
Yes.. it's a pain about how the .Net Framework loads up the control (web or windows) in design mode. It took me a couple of hits before I finally figured it out. In fact even though we have so much more power in making controls now with .Net, there's so much more planning to do. Well... at least that just means they still need programmers around. ;)

Say did you ever managed to give that winforms bug example I sent you to anyone?
Wednesday, 01 October 2003 13:26:47 UTC
One thing that bothers me is that it seems that we have to put default contructors in our base page (or winforms) classes so that the designer works.
Sometimes it doesn't make sense for my base page to have a constructor like this. Even more, sometimes I want it to be an abstract class.
Sergio Pereira
Wednesday, 01 October 2003 16:02:51 UTC
I ran into a similar problem with WinForms controls. Turns out (for the same reason) that none of the classes in your control hierarchy can be abstract, or everything goes berzerk because the desinger want to instantiate all the super classes. We had to opt for virtual methods and just hope everyone remembers to override them.
Friday, 03 October 2003 18:35:10 UTC
Instruct the IDE not to do this by including the following attribute in front of your properties called by the constructor:

[Browsable(false),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

Friday, 04 March 2005 21:15:00 UTC
If you set your reference to that project to Copy To Local = False, it will stop this behavior.
Jeff B
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.