2009-02-25 117 views
0

我有一個工作正常的用戶控件。它是這樣宣佈的。ASP.NET用戶控件繼承

public partial class DynamicList : System.Web.UI.UserControl 
{ 
     protected static BaseListController m_GenericListController = null; 

     public DynamicList() 
     { 
      m_GenericListController = new GenericListController(this); 
     } 
} 

現在我想覆蓋這個控制,所以我可以改變一些屬性。我已經創建了一個這樣的課程。

public partial class JobRunningList : DynamicList 
{ 
    public JobRunningList() 
    { 
     m_GenericListController = new JobListController(this); 
     (m_GenericListController as GenericListController).ModuleId = 14; 
    } 
} 

雖然當我現在使用JobRunningList控件導致可預測的錯誤結果時,DynamicList中的控件似乎並未創建。 DynamicList UserControl上有一個ListView和一些其他控件。看起來這些不是在使用JobRunningList時創建的。這有什麼祕訣嗎?

+0

我不認爲你會得到這個有限的信息的任何好的答案(看我證明是錯的)。我建議編輯你的問題來添加你想要覆蓋的所有代碼,也可能涉及到所有類的代碼...... – Will 2009-02-25 20:27:10

回答

0

無聊的解決方法是將JobRunningList設置爲包含DynamicList的普通舊用戶控件,並在其OnLoad中設置內部控件的屬性。如果DynamicList具有許多其他您想要從頁面訪問的屬性,那麼這很尷尬,因爲JobRunningList必須定義它自己的匹配屬性。回到繼承方法,然後...

DynamicList類只包含邏輯後面的代碼,所以如果您希望第二個控件重用第一個邏輯後面的邏輯但您提供新的它自己的UI。

將.ascx文件中的標記編譯爲繼承DynamicList的另一個類,因此如果您可以讓JobRunningList類繼承該類而不是DynamicList,那麼您將得到所需的結果。這個類從文件名獲得一個默認名稱,但是你可以避免猜測,通過在控制指令中設置一個ClassName來代替自動名稱。

取像

<%@ Control Language="C#" AutoEventWireup="true" 
      CodeFile="HelloControl.ascx.cs" Inherits="HelloControlBase" 
      ClassName="MyControls.HelloControl" %> 

Hello <%= Name %> 

一個簡單的基本控制用平平無奇的代碼隱藏像

public partial class HelloControlBase : System.Web.UI.UserControl 
{ 
    public string Name 
    { 
     get; 
     set; 
    } 
} 

現在我們要重寫Name屬性在新的控制。首先,我們需要HelloAlice.ascx

<%@ Control Language="C#" AutoEventWireup="true" 
      CodeFile="HelloAliceControl.ascx.cs" 
      Inherits="HelloAliceControl" %> 

沒有太多看到這裏,因爲我們要離開所有的工作到原來的ASCX。現在在代碼隱藏,

public partial class HelloAliceControl : MyControls.HelloControl 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     this.Name = "Alice"; 
    } 
} 

我們只是繼承MyControls.HelloControl並設置Name屬性,它看起來像我們完成了。

問題是知道何時MyControls.HelloControl可見。只要派生控件與父控件位於同一個目錄中,您可能會確定,否則很容易出現構建錯誤,抱怨該類不存在,因爲父控件尚未編譯。

0

如果我理解正確,您希望接口是相同的。在這種情況下,我會創建一些屬性。也許只是一個簡單的枚舉,即ListType。