简体   繁体   中英

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 renders [ 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:

替代文字

Run time result:

替代文字

您要查找的属性称为DesignMode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM