简体   繁体   中英

How To Compare My Actual MasterPage

I have 2 MasterPage in my project.

The MasterPage to common pages and the 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.

Ex:

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

How do I test it?

The is operator is handy for checking types.

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:

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 . However, if...else if would solve this:

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:

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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