简体   繁体   中英

UserControl to behave like a panel in asp.net

I have several custom user controls (100+) that all have a common formatting involved which basically requires us to wrap the user control in a panel control on the destination page. For simplicity, I wanted to create a base control that handles this, as almost all of the base features are the same for each of the custom controls.

What I have done to accomplish this is to create a base class, inside that base class I create a private panel control, and then I override the Render to generate the panel pre/post tags around the base.Render.

Now this works great as all of the user controls that we care about that are inheriting this and the few formatting items that we have exposed to the inherited controls work as expected (Width, CssClass, etc).

What I would really like is to expose all of the panel control items to the inherited control through the base class, but without having to right a property/method to expose each element.

Any ideas on what the best approach is for this? I just don't want to implement each and every panel property/method manually. We use the design time attributes as well (number one is CssClass and Width) but we have been entending the user of design time attributes...

What we have works, just looking for a easier/better solution.

using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public class BaseUserControl : System.Web.UI.UserControl
{
    private Panel _panel;
    private bool _isPanelLoaded;

    public Panel Panel
    {
        get
        {
            if (_panel == null)
            {
                _panel = new Panel();
                _isPanelLoaded = true;
            }
            return _panel;
        }
    }

    public BaseUserControl()
    {

    }

    protected override void Render(HtmlTextWriter writer)
    {
        if (_isPanelLoaded)
        {
            Panel.RenderBeginTag(writer);
            base.Render(writer);
            Panel.RenderEndTag(writer);
        }
        else
        {
            base.Render(writer);
        }
    }

    public Unit Width
    {
        get
        {
            if (Panel.Width.IsEmpty)
            {
                return 0;
            }
            return Panel.Width;
        }
        set
        {
            Panel.Width = value;
        }
    }

    public string CssClass
    {
        get
        {
            return Panel.CssClass;
        }
        set
        {
            Panel.CssClass = value;
        }
    }
}

In order to expose all the properties, you have to inherit from the Panel control. Since you have a panel property, you could set the properties on that property. To define them in markup is the challenge... with most simple objects, if you give the property an attribute of <PersistenceMode(PersistenceMode.Attribute)> , you would be able to access the properties in the sytnax of Panel-CssClass , but I don't know if that will work for you, since Panel is a control. Good to try.

You're essentially talking about writing properties once that wrap the attributes you want, so it may be best just to do that, if the former solution doesn't work.

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