简体   繁体   English

UserControl的行为类似于asp.net中的面板

[英]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. 我有几个自定义用户控件(超过100个),都涉及一种通用格式,这基本上需要我们将用户控件包装在目标页面上的面板控件中。 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. 为此,我要做的是创建一个基类,在该基类内创建一个私有面板控件,然后重写Render以在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). 现在,这非常有用,因为我们关心的所有用户控件都继承了此控件,而我们已经向继承的控件公开的少数格式化项可以按预期工作(Width,CssClass等)。

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... 我们也使用设计时间属性(第一个是CssClass和Width),但我们一直在吸引设计时间属性的用户...

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. 为了公开所有属性,您必须从Panel控件继承。 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. 要在标记中定义它们是一个挑战...对于大多数简单对象,如果为属性赋予<PersistenceMode(PersistenceMode.Attribute)>属性,则可以访问Panel-CssClass的语法中的属性,但是我不知道这对您是否有用,因为Panel是一个控件。 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. 实质上,您是在谈论编写包含所需属性的属性,因此,如果前一种解决方案不起作用,则最好这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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