Use Master Page functions in a Content Page?

D

DevNll2002

Is there a way to call Master Page functions (i.e. classes or voids)
from a Content Page?

Similarly, is there a way to reference Master Page controls from a
public Class--such as one that resides under the App_Code directory?

Either way would accomplish my goal, which is to use one central
function instead of putting it in each Content Page, because it acts
upon controls in that Content Page.

For example, one such function dynamically sets MinimumValue and
MaximumValue for a RangeValidator. This needs to be done in several
pages according to differnt rules; if centralized, the rules could be
passed to a function as parameters.

My Content Pages currently can both reference public Classes, and they
can reference controls on the Master Page, but neither of those
abilities reach my goal.

MSDN doesn't mention anything about this that I can find, nor any
posts.

Thanks for any help!
 
M

Michael Nemtsev [MVP]

Hello (e-mail address removed),

You need either to reference the Master Page through the Page.Master property
or set the @MasterType directive in your aspx file

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
 
D

DevNll2002

Thanks for your input, gentlemen!
Unless I'm missing something, the issue remains because:

- A method within Global.asax is not able to access controls on the
Master Page, just as is the case of a method in a public Class.

- My Content Pages can already reference the Master Page through the
Page.Master property and the @MasterType directive, but that only
seems to work for *controls* on the Master Page, and not *methods*.

The need is to have a publicly-accessible method which can act upon
controls in the Content Pages.
The methods work now by using FindControl(), but they must exist in
each Content Page.

Snippet of code from a Content Page:

ContentPlaceHolder cph =
(ContentPlaceHolder)Master.FindControl("cntDataEnter");
DropDownList dd = (DropDownList)cph.FindControl(DropdownName);

string DropdownName is passed into this method for any DropDownLists
on the Content Page, and dd can then be manipulated.

The question is, can this method be made public, so that it only has
to exist once, instead of on each Content Page.

Thanks!
 
B

Ben Amada

- My Content Pages can already reference the Master Page through the
Page.Master property and the @MasterType directive, but that only
seems to work for *controls* on the Master Page, and not *methods*.

With a MasterType directive like:

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

You can call a method in the master page from a content page with:

Master.MethodNameInMaster()

( note there is no "Page" preceding "Master" )
 
D

DevNll2002

You can call a method in the master page from a content page with:
Master.MethodNameInMaster()
( note there is no "Page" preceding "Master" )

I just needed that confirmation for the light to dawn...my problem was
that the methods in the Master Page were all declared 'protected' so
they weren't visible in the Content Pages. Declaring them public
fixes that.

Thanks to all for the insight, and happy post-Resurrection day!
 
D

DevNll2002

Here's a followup issue on the same subject:

Method in MyMaster.master.cs:
public void Logout(object sender, EventArgs e)
{
Response.Write("You are logged out!");
}


Code in MyContent.aspx:
<%@ MasterType VirtualPath="~/MyMaster.master" %>
...
<asp:Button ID="btnLogout" runat="server" Text="Logout"
OnClick="Master.Logout" />


Access the page, and in big red letters:
Server Application Unavailable


Application log in Event Viewer shows:
Event Type: Error
Event Source: ASP.NET 2.0.50727.0
Event Category: None
Event ID: 1000
Description: aspnet_wp.exe (PID: 3368) stopped unexpectedly.

Happens every time that 'Master.' is used directly in the .aspx code.

A workaround is to call Master.Logout from within a MyContent.aspx.cs
method, then reference the latter method in MyContent.aspx.

MyContent.aspx *is* seeing the method in MyMaster.master.cs, because
it will throw errors for certain things like leaving out the overloads
in the Logout method declaration.

Is there a correct syntax for calling a Master Page method directly
from inline code, or should it just not be done?

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top