0

I'm sure this has a simple answer, but I can't find it. I need to create an ASP.NET control that doesn't render anything. This is similar to an ObjectDataSource that shows up as a gray box in the aspx design mode. Until now I have only created controls that DO render and I can't find what property, attribute, override, etc. will prevent rendering during design. Any pointers?

Edit: Just to clarify, by simply inheriting from Control, it renders [ TypeName "ControlId" ]. I want it to render as the gray box that says TypeName - ControlId.

Nelson Rothermel
  • 9,436
  • 8
  • 62
  • 81

2 Answers2

2

OK well I played around with it and you're right, it's not trivial.

Turns out VS doesn't actually execute .ascx user control code when it's shown in the designer, it only parses the markup, so you can't conditionally change the control.

However, that limitation is not active when you use a real ASP.NET Server Control. Just add a new project to your solution of that type and in your .cs file, overrite RenderContents:

    protected override void RenderContents(HtmlTextWriter output)
    {
        if (DesignMode)
            output.Write("<table style='border-color:Black;background-color:Gray'><tr><td width=300px height=100px style='vertical-align:middle;text-align:center'>I'm your design time box.</td></tr></table>");
        else
            output.Write("Runtime text lalala");
    }

Then in your .aspx file, you just add the control:

<body>
    <form id="form1" runat="server">
    <div>
        <cc1:ServerControl1 ID="ServerControl1" runat="server" />
    </div>
    </form>
</body>

Design time result:

alt text

Run time result:

alt text

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • +1 for the effort, but do I really need to create the box manually? I am using a server control (no ascx), so that part is not a problem. Maybe I was wrong to assume that the gray box is a standard box generated by Visual Studio when the control doesn't have a UI component? Does `ObjectDataSource` really manually generate the HTML? Maybe I should pull out Reflector and dig in. – Nelson Rothermel Jan 05 '11 at 18:51
  • It does seem like a standard box. Drop in any control, set a property that doesn't exist and you get a similar box with `Error Creating Control...` – Nelson Rothermel Jan 05 '11 at 19:08
  • I wouldn't know where to begin looking for it though, and it's not too difficult to get a table to look similar enough to it! – Blindy Jan 05 '11 at 21:09
0

The property you are looking for is called DesignMode

citronas
  • 19,035
  • 27
  • 96
  • 164
  • I have used that in the past to create custom rendering logic during design. Can you explain how I would use that property to cause the gray box to render? Here is an example of the "gray box" I keep referring to: http://www.shotdev.com/wp-content/uploads/2010/01/aspnet_objectdatasource12.jpg – Nelson Rothermel Jan 05 '11 at 17:19
  • Check the property in your `Load` event, and conditionally make the design or runtime parts of your control visible. You would have both drawn and ready to go in the control itself. – Blindy Jan 05 '11 at 17:28
  • @Blindy: I have tried overriding the `Render*` methods. I have cleared `Controls`, overriden `CreateControlCollection` and returned `new EmptyControlCollection(this)`. Nothing seems to work. I understand how `DesignMode` works and how I can use it to add custom logic. But once I have that custom logic, how do I make the gray box appear? – Nelson Rothermel Jan 05 '11 at 17:39
  • Have you tried assigning a ControlDesigner? See http://stackoverflow.com/questions/3669582/how-to-hide-the-inner-controls-of-a-usercontrol-in-the-designer for some links on this topic – citronas Jan 05 '11 at 17:42