Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Use Master Page functions in a Content Page?

12 views
Skip to first unread message

DevNl...@gmail.com

unread,
Mar 22, 2008, 3:07:10 PM3/22/08
to
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!

Peter Bromberg [C# MVP]

unread,
Mar 22, 2008, 5:42:00 PM3/22/08
to
It might be easier to put the methods in Global.asax as public static
methods. Then you can get to them from anywhere without a lot of bother, e.g.

Global.SetMaximumWhatever(0,10);

-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net

Michael Nemtsev [MVP]

unread,
Mar 23, 2008, 2:13:56 AM3/23/08
to
Hello DevNl...@gmail.com,

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

DevNl...@gmail.com

unread,
Mar 23, 2008, 6:13:48 PM3/23/08
to
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!


Ben Amada

unread,
Mar 23, 2008, 10:12:28 PM3/23/08
to
<DevNl...@gmail.com> wrote:

> - 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" )

DevNl...@gmail.com

unread,
Mar 24, 2008, 9:47:05 AM3/24/08
to
> 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!

DevNl...@gmail.com

unread,
Apr 1, 2008, 8:25:54 PM4/1/08
to
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.

0 new messages