1

How do I set a property in a master page from a class in app_code?

my master page:

private int styleGroup;
public int StyleGroup
{
    get { return styleGroup; }
    set { styleGroup = value; }
}

how to I set StyleGroup from a class in the app_code? something like this, but it doesn't work:

Page currentPage = (Page)HttpContext.Current.Handler;
currentPage.Master.StyleID = 5;
2

2 Answers 2

1

You have to cast the MasterPage of current to you MasterPage Class, something like this

Page currentPage = (Page)HttpContext.Current.Handler;
((YourMasterPageClass)currentPage.Master).StyleID = 5;
2
  • How can I inherit the Master page class? because if do it your way it says 'the type or name space name SiteMaster could not be found' SiteMaster is my master page class.
    – vts
    Dec 28, 2011 at 20:22
  • Your Master Page class must be 'Site', that Inherits from MasterPage, so you should put something like: [VB] Dim Master As Site = DirectCast(DirectCast(HttpContext.Current.Handler, Page).Master, Site) Master.StyleGroup=5 [C#] Site Master = (Site)((Page)HttpContext.Current.Handler).Master; Master.StyleGroup=5;
    – Piyey
    Dec 28, 2011 at 20:36
0

Pass a Page pointer to the app_code class function or set a Page member in the class in app_code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.