[英]How To Compare My Actual MasterPage
我的項目中有2個MasterPage。
將MasterPage to common pages
,將MasterPage to PopUp pages
。
我有一個被所有頁面繼承的BasePage
類,在BasePage中,我需要驗證哪個是實際使用的MaterPage。
例如:
if(Master.GetType() == typeof(Master)
....
我該如何測試?
is
運算符非常便於檢查類型。
如果兩個母版(我將它們稱為MasterPage和MasterPagePopup)是從一個共同的祖先(Page?)繼承而來的,而不是彼此繼承,則可以執行以下操作:
if(Master is MasterPage)
{ do some stuff; }
if(Master is MasterPagePopup)
{ do other stuff; }
唯一的陷阱是,一個主控是否從另一個主控繼承。 如果MasterPagePopup是繼承形式母版,那么上述兩種情況下,會因為他既是一個母版和MasterPagePopup是MasterPagePopup如此。 但是, if...else if
將解決此問題:
if(Master is MasterPagePopup)
{ do other stuff; }
else if(Master is MasterPage) // popup is already handled and will not hit this
{do some stuff; }
檢查MasterPage類型的最簡單方法是使用is
關鍵字:
if (this.Master is MasterPageCommon) {
} else if (this.Master is MasterPagePopup) {
}
你應該能夠做
if(page.Master is PopUpMaster)
{
//Do Something
}
else if (page.Master is NormalMaster)
{
//Do Something
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.