ok,
I am new to master pages. I have created a public function called setMenu in the cs file.

I can call this function fine from about half of my pages, but when i try to call it from the other half, it won't compile and I get

'System.Web.UI.MasterPage' does not contain a definition for 'setMenu'

I'm calling it exactly the same way in all the pages. Any ideas?

Dani AI

Generated

Nice catch — that is the usual cause. The compiler error happens because, by default, Page.Master is seen as the base type System.Web.UI.MasterPage, which does not declare your custom setMenu method. The MasterType directive you added makes the compiler treat Master as your specific master-page class so the call compiles.

If you prefer not to add a MasterType directive on every content page, two common alternatives:

var m = Master as MyMaster;
if (m != null) m.setMenu();

or define an interface on the master and cast to that interface:

IMenuHost host = Master as IMenuHost;
if (host != null) host.setMenu();

Checklist for the common causes if a page still cannot see the method:

  • Verify the master-page method is declared public in the master code-behind.
  • Confirm the master page class name/namespace matches the Inherits in the .master file (mismatches make the runtime treat it as the base MasterPage).
  • If you use the directive that strongly-types the master, ensure the VirtualPath or TypeName is correct (and fully qualified if using a namespace).
  • Rebuild the project after changes (designer files / compiled assemblies can be out of sync).
  • For Web Site vs Web Application projects: ensure code-behind compilation model is correct (CodeFile vs CodeBehind) and the master class is actually compiled into the assembly the pages use.

For maintainability prefer a strongly typed Master (directive) or an interface. Casting tightly couples pages to a concrete master type; an interface or events keeps pages decoupled and easier to test.

ok, figured out the problem. needed the following line in the aspx page. Evidently, this makes functions from the master accessible by the content page

<%@ MasterType VirtualPath="~/MyMaster.master" %>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.