Inheritance from the base class System.Web.UI.UserControl

First, I wonder if this is possible. I read light grunts on the internet about this, but I was not quite sure.

My scenario: I have a base chart class that has some methods that 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 a small premium for this BaseChart - setting background colors, etc. All charts that inherit BaseChart must use this starting markup and be able to build on it.

Then I would 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 non-markup web user control, for example. "HistoricalLineChart.ascx"

The problem is that Chart1 is undefined when in the HistoryLineChart area. Is there something I'm missing here?

Thank.

+3
3

, BaseChart BaseChart. , BaseChart. , HistoricalLineChart , BaseChart, . , , - Composite Control Custom Server Control ( UserControl). , , , .

: http://msdn.microsoft.com/en-us/library/3257x3ea(v=VS.100).aspx

:

  • CompositeControl.
  • CreateChildControls. (, ).
  • : Render. , . RenderControl , , . , , .

:

+1

, ( ), , , , .

1) , , , UserControl. , usercontrol , . , , , , .

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) UserControl, MyUserControl UserControl,

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

, , , . , , UserControl ( ), WebControl ( ), , .

+2

You can create a property protectedin BaseChartthat displays the chart.

0
source

Source: https://habr.com/ru/post/1795339/


All Articles