我有兩個.ascx文件,一個被加載到另一個.ascx控件中的佔位符中。如何在另一個.ascx中從.ascx獲取變量?
所以我必須:
ParentControl
- >ChildControl
我想從ChildControl訪問ParentControl得到可變數據。
到目前爲止,我有:
ParentControl.ascx
public UserList GetFunction
{
get
{
return someVariable;
}
}
而且在:
ChildControl.ascx
Protected void Page_Load(object sender, EventArgs e){
ParentControl page = new ParentControl;
string newVariable = page.GetFunction.someOtherVariable;
}
我要去哪裏WR ong,當我在父類中返回someVariable時,它具有我需要的,但是當我嘗試在ChildControl頁面中獲取它時.GetFunction返回null。
讓我知道你是否需要更多信息。
更新應答:
我已經maanged通過它周圍的工作來解決這個問題:
在父類叫我在子類中定義的函數和解析我通過所需要的值。
ParentClass.ascx
protected void Page_Load
{
ControlIWantToGetInformationTo.SetInfo(info);
}
ChildClass.ascx
public void SetInfo(Info info)
{
string someString = info.TheVariableWithin;
}
一些用戶已經使用了一段代碼提到,
this.parent;
,但因爲我使用Sitecore的CMS的這.parent返回一個不需要的值,因爲該頁面在運行sitecore時不是物理的。
感謝所有的幫助,很高興我設法解決了這個問題:)。
您正在使用的方法(父頁/控件使用特定數據來配置其子項)是一種非常常見的,可接受的和預期的做法。說實話,它比封裝方式好得多,比讓孩子訪問其父母,假設它是某種類型,並從中提取數據要好。所以海事組織,你沒有解決這個問題 - 你按照你「應該」的方式解決它。 – mikemanne
感謝您的最佳做法提示,沒有意識到我會期待在未來做到這一點:)。 – Anicho