How do I access Master page property from .cs file? I tried the following code but I couldn't access it.Please let me know.
public int TypeID
{
get
{
return Convert.ToInt32(this.ViewState["TypeID"]);
}
set
{
this.ViewState.Remove("TypeID");
this.ViewState.Add("TypeID", value);
}
}
var pageHandler = HttpContext.Current.CurrentHandler;
if (pageHandler is System.Web.UI.Page)
{
typeId = Convert.ToInt32((System.Web.UI.Page)pageHandler).Master.TypeID;
}
The Master
property of a page is typed as System.Web.UI.MasterPage
. In order to see the TypeId
property, you need to cast the Master
to the type of your specific master page.
var page = (System.Web.UI.Page)pageHandler
var master = (MyMasterType)page.Master; //Replace MyMasterType with the class name from your masterpage.cs file.
var typeId = master.TypeId;
You'll want to be careful casting it to a specific master type if you have multiple master types in your application or if some pages don't have a master page.