How to use a method in a Master Page from a Content Page
Asked Answered
B

3

8

I am writing an ASP.NET 4 application with C#. I have a Master Page, inside of which I have the following method:

public void DisplayMessage(string input)
{
   Label myMessageDisplayer = (Label)FindControl("uxMessageDisplayer");
   myMessageDisplayer.Text = input;
}

Can I call this method from a content page?
At the moment, I use in Content Page this code:

Master.DisplayMessage("Item has been inserted.");

And I receive this error:

'System.Web.UI.MasterPage' does not contain a definition for 'DisplayMessage' and no extension method 'DisplayMessage' accepting a first argument of type 'System.Web.UI.MasterPage' could be found (are you missing a using directive or an assembly reference?)

Any help will be appreciated.

Bambibambie answered 21/2, 2011 at 16:32 Comment(1)
Which VS version are you using?Aristate
V
7

You can use casting to get your master page type, as others have shown, or you can add the MasterType directive to your page markup (at the top, where the standard <%@ Page %> directive is):

<%@ MasterType TypeName="YourNamespace.YourMasterPageType" %>

Then in your page code, you can have just what you have in your example:

Master.DisplayMessage("Item has been inserted.");

The MasterType directive is available from .NET 2 upwards.

Vickey answered 21/2, 2011 at 16:44 Comment(1)
"The MasterType directive is available from .NET 2 upwards." As are Master Pages. :-)Fraase
L
6

You need to cast the Master to the actual Masterpage type

// MyMasterPage is the type of your masterpage
((MyMasterPage)Master).DisplayMessage("Item has been inserted.");

You can also add a MasterType directive to the top of your content page to achieve the same thing:

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

Then you can use:

Master.DisplayMessage('Item has been inserted.');
Lymphocyte answered 21/2, 2011 at 16:34 Comment(8)
Hi where I can inser <%@ MasterType VirtualPath="~/MyMasterPage.master" %> in the content page or master page? thanksBambibambie
That's placed in the content page.Sophist
@GlbboK - add it to your content page. Then you can just use Master.DisplayMessage('...');Lymphocyte
I try Graham, but i get an error again Error 1 The type or namespace name 'MasterPages' does not exist in the namespace 'WebProject.Web.Cms.AdminCms' (are you missing an assembly reference?)... what im doing wrong???Bambibambie
I am using this code: <%@ MasterType virtualPath="~/Cms/AdminCms/MasterPages/AdminCms.Master"%>Bambibambie
@BlbboK - is the path to AdminCms.Master correct? Does it work without the MasterType directive and casting in your code to the correct type?Lymphocyte
@BlbboK - Is this a website or web application project?Lymphocyte
OK I made it, I have to fix just the path really thanks fot hitsBambibambie
S
0

I made/try this:

My Control (MenuEsquerdo.ascx)

<%--<%@ MasterType VirtualPath="~/Site.Master" %>--%>
<%--<%@  MasterPageFile="~/Site.Master" %>--%>
<%--<%@ MasterType TypeName="Delivery.SiteMaster" %>--%>
<%--<%@ MasterType VirtualPath="~/Site.Master" %>--%>
<%@ MasterType TypeName="Delivery.SiteMaster" %>

My Designer Control (MenuEsquerdo.ascx.designer.cs)

public new Delivery.SiteMaster Master {
            get {
                return ((Delivery.SiteMaster)(base.Master)); // 'System.Web.UI.UserControl' does not contain a definition for 'Master'
            }
        }

And my code behind (MenuEsquerdo.ascx.cs)

RepeaterListaMenuEsquerdoCtr.DataSource = this.Master.ClassMercadorias.Take(20);
RepeaterListaMenuEsquerdoCtr.DataBind();

Know, how I can fix this: 'System.Web.UI.UserControl' does not contain a definition for 'Master'?

Sassenach answered 14/1, 2013 at 19:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.