[英]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: 基本上:
CompositeControl
. CompositeControl
继承。 CreateChildControls
. CreateChildControls
。 In this method, you can add all of your child controls (like your chart). Render
. Render
。 Override this if you need custom markup in addition to the child controls. 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.