2011-10-19 29 views
1

此問題適用於ASP.NET專家。它使我瘋狂。在ASP.NET中的代碼隱藏中訪問usercontrols

我已經繼承了ASP.NET Web窗體應用程序。此應用程序使用嵌套用戶控件的複雜 結構。雖然複雜,但在這種情況下似乎是必要的。無論如何,我有一個使用單個UserControl的頁面。我們將調用此UserControl 根控件。該用戶控件定義如下:

widget.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="widget.ascx.cs" Inherits="resources_userControls_widget" %> 
<div> 
    <asp:Panel ID="bodyPanel" runat="server" /> 
</div> 

widget.ascx.cs

public partial class resources_userControls_widget : System.Web.UI.UserControl 
{ 
    private string source = string.Empty; 
    public string Source 
    { 
     get { return source; } 
     set { source = value; } 
    } 

    private string parameter1 = string.Empty; 
    public string Parameter1 
    { 
     get { return parameter1; } 
     set { parameter1 = value; } 
    } 

    private DataTable records = new DataTable(); 
    public DataTable Records 
    { 
     get { return records; } 
     set { records = value; } 
    } 

    protected override void OnPreRender(EventArgs e) 
    { 
     base.OnPreRender(e); 
     UserControl userControl = LoadControl(source) as UserControl; 
     if (parameter1.Length > 0) 
      userControl.Attributes.Add("parameter1", parameter1); 
     bodyPanel.Controls.Add(userControl); 
    } 

    private void InsertUserControl(string filename) 
    { 
    } 
} 

在我的申請,我在下面使用widget.ascx way: page.aspx

<uc:Widget ID="myWidget" runat="server" Source="/userControls/widgets/info.ascx" /> 

page.aspx.cs

protected void Page_Load(object sender, EventArgs e) 
{ 
    DataTable table = GetData(); 
    myWidget.Records = table; 
} 

請注意info.ascx是如何設置的,因爲我們希望在這種情況下加載的用戶控件。在這種情況下,這種方法是必要的。我刪除了無用的代碼,證明它專注於這個問題。無論如何,在info.ascx.cs我有以下幾點:

info.ascx.cs

protected void Page_Load(object sender, EventArgs e) 
{ 
    // Here's the problem 
    // this.Parent.Parent is a widget.ascx instance. 
    // However, I cannot access the Widget class. I want to be able to do this 
    // Widget widget = (Widget)(this.Parent.Parent); 
    // DataTable table = widget.Records; 
} 

我真的需要得到來自母公司的用戶控制的「記錄」屬性的值。不幸的是,我似乎無法從我的代碼隱藏中訪問Widget類。在編譯時有沒有關於UserControl可見性的規則,我不知道?如何從info.ascx.cs的代碼隱藏中訪問Widget類?

謝謝!

+0

只是好奇,是什麼this.Parent.Parent.ToString()報告是什麼? – Sabre

+0

Parent.Parent.ToString()顯示我試圖獲得的類的名稱(Widget)。這一切都是如此古怪。在運行時,我可以看到類名稱很好。但是,在開發/編譯期間,Widget在IntelliSense中不可用。如果我只是使用它,不考慮智能感知,編譯失敗。 –

回答

3

首先,您需要創建一個接口並將其實現到Widget用戶控件類。

例如,

public interface IRecord 
{ 
    DataTable Records {get;set;} 
} 

public partial class resources_userControls_widget : System.Web.UI.UserControl, IRecord 
{ 
... 
} 

而且在Info.ascx.cs的後臺代碼,

protected void Page_Load(object sender, EventArgs e) 
{ 
    // Here's the problem 
    // this.Parent.Parent is a widget.ascx instance. 
    // However, I cannot access the Widget class. I want to be able to do this 
    // Widget widget = (Widget)(this.Parent.Parent); 
    // DataTable table = widget.Records; 

    IRecord record=this.Parent.Parent; 
    DataTable table = widget.Records; 
} 
+0

非常漂亮。我會給它一個鏡頭,如果它的工作,我會記下這個職位。但我喜歡這個想法。 –

+0

對不起,夥計們,但IRecord記錄= this.Parent.Parent; - 這是一個愚蠢的,因爲家長有一個控制類型,不能實現任何自定義接口。 – apros

+0

它爲我工作。 –

0

在你的情況,也許最好使用一些像ViewState或Session的服務器對象。將其填充到頁面的DataTable中,並將其放入info.ascx用戶控件的Page_load事件處理程序中。

相關問題