简体   繁体   English

如何比较我的实际母版页

[英]How To Compare My Actual MasterPage

I have 2 MasterPage in my project. 我的项目中有2个MasterPage。

The MasterPage to common pages and the MasterPage to PopUp pages . MasterPage to common pages ,将MasterPage to PopUp pages

And I have a class BasePage that is inherited by all pages, and in BasePage I need to verify which is the actual MaterPage that is used. 我有一个被所有页面继承的BasePage类,在BasePage中,我需要验证哪个是实际使用的MaterPage。

Ex: 例如:

if(Master.GetType() == typeof(Master) .... if(Master.GetType() == typeof(Master) ....

How do I test it? 我该如何测试?

The is operator is handy for checking types. is运算符非常便于检查类型。

If the two masters (I will call them MasterPage and MasterPagePopup) are inherited form a common ancestor (Page?) and not one another, you could do something like this: 如果两个母版(我将它们称为MasterPage和MasterPagePopup)是从一个共同的祖先(Page?)继承而来的,而不是彼此继承,则可以执行以下操作:

if(Master is MasterPage) 
  { do some stuff; }
if(Master is MasterPagePopup)
  { do other stuff; }

The only gotcha is if one master is inherited from the other; 唯一的陷阱是,一个主控是否从另一个主控继承。 if MasterPagePopup is inherited form MasterPage, then both cases above would be true for MasterPagePopup as he IS both a MasterPage and MasterPagePopup . 如果MasterPagePopup是继承形式母版,那么上述两种情况下,会因为他既是一个母版和MasterPagePopup是MasterPagePopup如此。 However, if...else if would solve this: 但是, 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; }

The easiest way to check the type of your MasterPage is with the is keyword: 检查MasterPage类型的最简单方法是使用is关键字:

if (this.Master is MasterPageCommon) {

} else if (this.Master is MasterPagePopup) {

}

You should just be able to do 你应该能够做

if(page.Master is PopUpMaster)
{
   //Do Something
}
else if (page.Master is NormalMaster)
{
   //Do Something
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM