Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Composite Controls, Render and VS Designer

I have started to develop my own composite controls using VS.Net.  All is going well, and I can even see what the control looks like in the designer - until I start to modify the render() method.

But, since several of these are to be general purpose menus, who have elements displayed or hidden based on the user's access levels, I thought that Render() is the best place to set the visible attribute on many of the controls making up the overall custom control.  After all, Render() gets called after all processing of the .net page, and this is what actually generates the html - so just before I call RenderChildren() in my control's render method, set things visible or not.

But this now breaks the lovely control display in the designer.  Sure, this isn't earth shattering - but it would be nice if there was another way to safely set the various visible attributes.

Any suggestions?

Ken Ray
Wednesday, May 04, 2005

Test to see if you're in design mode, and render accordingly.  You can do this using the "Site" property of your control - which, despite what MSDN says, is not "the Web site to which the server control belongs" :)

This is typically done as follows:

bool isDesignMode = (Site != null) && Site.DesignMode;

Joe
Wednesday, May 04, 2005

Brilliant.  Thanks for that.

Ken Ray
Wednesday, May 04, 2005

Well, I spoke too soon.  I can't seem to get the Site and DesignMode to work.  Here is the code:

        If Me.Site Is Nothing Then
            bIsDesignMode = True
            System.Web.HttpContext.Current.Trace.Write("Login", "Render: No site found.")
        Else
            bIsDesignMode = Me.Site.DesignMode
            System.Web.HttpContext.Current.Trace.Write("Login", "Render: DesignMode = " & Site.DesignMode.ToString & ".")
        End If

And when I access the page (with trace turned on), the "Render: No site found" trace appears - there is no "Site" object.  I included an "Implements ISite" directive in the class declaration - am I missing something?

Ken Ray
Thursday, May 05, 2005

Your first line should be:

If Not Me.Site Is Nothing Then
...

You don't need Implements ISite in the class declaration.

Joe
Friday, May 06, 2005

*  Recent Topics

*  Fog Creek Home