[英]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.