首先,我想知道這是否可能。我在網上讀到關於這個的輕微抱怨,但我並不完全確定。繼承自System.Web.UI.UserControl基類
我的場景:我有一個基本圖表類,它有一些所有圖表都應該有的方法。
public partial class BaseChart : System.Web.UI.UserControl
{
public BaseChart()
{
}
public void ToggleLegend()
{
Chart1.Legends[0].Enabled = !Chart1.Legends[0].Enabled;
}
}
也有一些標記爲這個BaseChart - 設置背景顏色等,這些繼承BaseChart應該使用這個起點標記和能夠建立在其上的所有圖表。
話,我想這樣做:
public partial class HistoricalLineChart : BaseChart
{
public HistoricalLineChart()
: base()
{
}
public HistoricalLineChart(int reportID)
: base()
{
Chart1.Titles[0].Text = "Hello World";
}
}
其中HistoricalLineChart是沒有加價例如web用戶控制「HistoricalLineChart.ascx」
問題是在HistoricalLineChart的作用域中Chart1未定義。有什麼我在這裏失蹤?
謝謝。
我認爲創建自定義服務器控件是適合您的方式。 – TheGeekYouNeed 2011-02-28 22:05:01
嘿,所以只是看着這(我從來沒有與自定義服務器控件一起工作)。我注意到,在演練中,他們正在創建一個名爲「WelcomeLabel.cs」的文件。 當我創建我的UserControl時,我使用Page.LoadControl(「foo.ascx」)。我知道LoadControl不能在類上調用 - 這是沒有意義的。我能夠在自定義服務器控件上調用LoadControl嗎?看起來好像所有的'custom'都附加在LoadControl無用的類中。 – 2011-02-28 22:13:58
您無需爲自定義服務器控件使用「Page.LoadControl」。這只是部分類需要(它加載了ascx部分)。只需說出Controls.Add(new MyControl()) - 或者像任何類那樣實例化它並將它添加到控件集合中。 – 2011-02-28 22:40:02