开发者

Control that doesn't render during design

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 rende开发者_如何学Gors [ TypeName "ControlId" ]. I want it to render as the gray box that says TypeName - ControlId.


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:

Control that doesn't render during design

Run time result:

Control that doesn't render during design


The property you are looking for is called DesignMode

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜