Accessing MasterPage variable in content page
I am trying to reference a variable within my MasterPage but I am receiving errors.
I have tried
<%@ MasterType" %>
which gives the following error:
Compiler Error Message: CS0030: Cannot convert type 'IPAMIntranet.IPAMIntranetMaster' to 'ASP.ipamintranetmaster_master'
and
string tVar = ((MyNamespace.MyMasterPage)Master).variable
wh开发者_StackOverflowich gives the following error:
Unable to cast object of type 'ASP.ipamintranetmaster_master' to type 'IPAMIntranet.IPAMIntranetMaster'.
Does anyone know what is happening or am I missing something.
You need to specify the virtual path to the Masterpage in the content page.
<%@ MasterType VirtualPath="Master.Master" %>
From the looks of it, it seems that your master page either isn't the type IPAMIntranet.IPAMIntranetMaster
, or doesn't inherit from IPAMIntranet.IPAMIntranetMaster
, the only way to resolve this would be to make it inherit, or make sure the type is correct.
The MasterType
directive can take the whatever class the Master is castable to, it's mainly for intellisense. You can provide either the VirtualPath
to the Master or TypeName
, which can be the Master's class, a base class, or an interface, whichever is more appropriate for your situation.
I worked this out by using an interface instead.
精彩评论