Custom ASP.NET Container Control

前端 未结 8 1909
醉话见心
醉话见心 2020-12-14 20:50

I\'ve been trying to create a custom control that works exactly like the Panel control except surrounded by a few divs and such to create a rounded box look. I haven\'t been

8条回答
  •  Happy的楠姐
    2020-12-14 21:25

    If you don't want to inherit directly from WebControl instead of from Panel, the easiest way to do this is to decorate the class with the attribute [ParseChildren(false)]. Although at first glance this might suggest that you don't want to parse children, what the false actually indicates is that you don't want the children to be treated as properties. Instead, you want them to be treated as controls.

    By using this attribute, you get virtually all of the functionality out of the box:

    [ToolboxData("<{0}:RoundedBox runat=server>")]
    [ParseChildren(false)]
    public class RoundedBox : WebControl, INamingContainer
    {
        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            writer.Write("
    "); } public override void RenderEndTag(HtmlTextWriter writer) { writer.Write("
    "); } }

    This will allow you to add RoundedBox controls to your pages, and add children (either asp.net controls or raw html) that will be rendered inside your div.

    Of course, css would be added to correctly style the roundedbox class.

提交回复
热议问题