Hi

In my application i have used master page. I created a web form that DID NOT USE master page. Is it possible that from this form i can access master page controls. If yes then how can i access it?

Dani AI

Generated

Short answer: a page that does not use a master page has no Master instance, so it cannot directly access master-page controls. That is what and were pointing out — Page.Master will be null when the page isn’t using a master (Page.Master documentation).

If access to the master’s UI is required, make the page use the master. Two common ways:

  • Convert the page into a content page by setting MasterPageFile in the page directive.
  • Assign MasterPageFile at runtime inside Page_PreInit (must be done in PreInit) so the Master is created for the page. See the PreInit event notes for details (Page.PreInit).

Once the page has a master, prefer exposing functionality via public properties or methods on the master (or use the <%@ MasterType %> directive for a strongly-typed Master), rather than reaching into controls with FindControl. Example pattern:

  • Add a public property on the master that gets/sets the control value.
  • In the content page cast/use the strongly-typed Master to call that property.

If converting the page to use the master is not an option, use one of these alternatives:

  • Move shared UI into a user control (.ascx) and include it both in the master and in standalone pages.
  • Put shared logic/state into a base page class, a helper/service class, or session/application storage.
  • Use server-side events/data exchange rather than direct control access.

As concluded, switching the page to use the master (or using a shared control/service) is the practical solution.

Recommended Answers

All 3 Replies

If any page is not using master page, how you can get reference? I don't think so you can do this.

You can refer any form within the project in .net windows application but it is not possible in Web applications.I think the only solution is to use master page..

ok got it..thank you all for your help

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.