简体   繁体   English

继承自System.Web.UI.UserControl基类

[英]Inheriting from a System.Web.UI.UserControl base class

First off, I am wondering if this is possible. 首先,我想知道这是否可行。 I read slight grumblings around the internet about this, but I was not entirely sure. 我在互联网上读到了关于此的轻微抱怨,但我并不完全确定。

My scenario: I have a base chart class which has some methods which all charts should have. 我的场景:我有一个基础图表类,它有一些所有图表应该具有的方法。

public partial class BaseChart : System.Web.UI.UserControl
{
    public BaseChart()
    {
    }

    public void ToggleLegend()
    {
        Chart1.Legends[0].Enabled = !Chart1.Legends[0].Enabled;
    }
}

There is also some mark-up for this BaseChart -- setting background colors, etc. All charts which inherit BaseChart should use this starting mark-up and be able to build upon it. 此BaseChart还有一些标记 - 设置背景颜色等。所有继承BaseChart的图表都应使用此开始标记并能够在其上构建。

I would then like to do this: 我想这样做:

public partial class HistoricalLineChart : BaseChart
{
    public HistoricalLineChart()
        : base()
    {
    }

    public HistoricalLineChart(int reportID)
        : base()
    {
        Chart1.Titles[0].Text = "Hello World";
    }
 }

where HistoricalLineChart is a web user control with no mark-up eg "HistoricalLineChart.ascx" 其中HistoricalLineChart是没有标记的Web用户控件,例如“HistoricalLineChart.ascx”

The problem is that Chart1 is undefined when in HistoricalLineChart's scope. 问题是,在HistoricalLineChart的范围内,Chart1未定义。 Is there something that I am missing here? 这里有什么我想念的吗?

Thanks. 谢谢。

Though usually one ends up just making a custom control in this situation (as the other answer suggest), and this is good for many reasons, there are a couple other approaches that may be useful in situations where complicated markup makes a server control infeasible. 虽然通常最终只是在这种情况下进行自定义控制(正如另一个答案所示),这有很多原因,但在复杂标记使服务器控制不可行的情况下,还有一些其他方法可能会有用。

1) Create a base class that has all the functionality common to your implementations and inherits UserControl. 1)创建一个基类,它具有您的实现共有的所有功能,并继承UserControl。 If you really want to include markup as part of the base class, you could put it in a separate usercontrol with no code, and load it from the abstract class, though this seems a little ugly. 如果你真的想要将标记作为基类的一部分包含在内,你可以将它放在一个没有代码的单独的usercontrol中,并从抽象类中加载它,尽管这看起来有点难看。 If the markup that is shared is simple, though, just render it from code instead. 但是,如果共享的标记很简单,只需从代码中渲染它。

public abstract class MyUserControl: UserControl
{
     public Chart Chart1;
     public void ToggleLegend()
    {
        Chart1.Legends[0].Enabled = !Chart1.Legends[0].Enabled;
    }
    public override void CreateChildControls()
    {
        Controls.Add(Page.LoadControl("path/to/mymarkup/control"));
        // or add them in code
        BuildBaseControls();
    }
}

2) Create UserControl implementations that inherit MyUserControl instead of UserControl, and add the markup 2)创建继承MyUserControl而不是UserControl的UserControl实现,并添加标记

public partial class HistoricalLineChart : MyUserControl
{
    public HistoricalLineChart(int reportID)
        : base()
    {
        Chart1.Titles[0].Text = "Hello World";
    }
}

You could also create an interface that describes any controls that should appear in the markup and implement that. 您还可以创建一个界面来描述应该出现在标记中并实现该控件的任何控件。 This is nice because it gives you a construct that is applicable to either a UserControl (where the controls are defined in markup) or a WebControl (where the controls are created in code), leaving the actual details of the markup to each implementation, but letting you share the functionality. 这很好,因为它为您提供了一个适用于UserControl(其中控件在标记中定义)或WebControl(其中控件是在代码中创建)的构造,将标记的实际细节留给每个实现,但是让你分享功能。

Unfortunately the markup portion of BaseChart is not actually part of the BaseChart class. 不幸的是, BaseChart的标记部分实际上并不是BaseChart类的一部分。 The markup is part of a class that gets created when you compile and it inherits from BaseChart . 标记是在编译时创建的类的一部分,它继承自BaseChart So HistoricalLineChart only contains what you've explicitly set in BaseChart and none of the markup. 因此, HistoricalLineChart仅包含您在BaseChart明确设置的内容, BaseChart包含任何标记。 The only way I know to work around this is to use a Composite Control or Custom Server Control (vs a UserControl).It's a bit more of a pain since you have to add your child controls programmatically, but should do what you want. 我知道解决这个问题的唯一方法是使用复合控件或自定义服务器控件(与用户控件相比)。由于你必须以编程方式添加子控件,但是应该按照自己的意愿行事,这有点痛苦。

Here is an example: http://msdn.microsoft.com/en-us/library/3257x3ea(v=VS.100).aspx 这是一个例子: http//msdn.microsoft.com/en-us/library/3257x3ea(v = VS.100).aspx

Basically: 基本上:

  1. Inherit from CompositeControl . CompositeControl继承。
  2. Override CreateChildControls . 覆盖CreateChildControls In this method, you can add all of your child controls (like your chart). 在此方法中,您可以添加所有子控件(如图表)。
  3. Optional: Override Render . 可选:覆盖Render Override this if you need custom markup in addition to the child controls. 如果除了子控件之外还需要自定义标记,请重写此项。 You can output your custom markup plus call RenderControl on all of your child controls to tell them where to render their output. 您可以输出自定义标记,并在所有子控件上调用RenderControl ,以告知它们在何处呈现其输出。 If you don't override this method at all, then the composite control will render out the child controls in the order that they are in the controls collection. 如果您根本不重写此方法,则复合控件将按照它们在控件集合中的顺序呈现子控件。

Here are a couple more tutorials: 这里有几个教程:

您可以在BaseChart中创建一个公开图表的protected属性。

暂无
暂无

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

相关问题 一个类可以继承System.Web.UI.Page和System.Web.UI.UserControl吗? - Can a class inherit from both System.Web.UI.Page and System.Web.UI.UserControl? 如何创建采用“ System.Web.UI.Page”和“ System.Web.UI.UserControl”的通用类? - How to create Generic Class which takes “System.Web.UI.Page” and “System.Web.UI.UserControl”? 继承自ASP.NET ListView,“此处不允许,因为它没有扩展类'System.Web.UI.UserControl'” - Inherit from ASP.NET ListView, “is not allowed here because it does not extend class 'System.Web.UI.UserControl'” 这里不允许使用,因为它不会扩展类'System.Web.UI.UserControl' - is not allowed here because it does not extend class 'System.Web.UI.UserControl' 关于发布错误-此处不允许这样做,因为它不会扩展类'System.Web.UI.UserControl' - On Publishing error - is not allowed here because it does not extend class 'System.Web.UI.UserControl' 我的控件“这里不允许,因为它没有扩展类'System.Web.UI.UserControl'” - My control is "not allowed here because it does not extend class 'System.Web.UI.UserControl'" 无法将类型'System.Web.UI.Control'隐式转换为'System.Web.UI.UserControl - Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.UserControl 基类继承自另一个基类? - Base class inheriting from another base class? 使用Visual Studio设计器时继承UserControl基类时出错 - Error inheriting UserControl base class while working with Visual Studio designer 从Base继承的基类属性 - Base Class Property Inheriting from Base
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM