Click here to Skip to main content
15,888,908 members
Home / Discussions / ASP.NET
   

ASP.NET

 
QuestionHow can I access a method on a child page from its master page ? Pin
Gsalles10-May-11 9:42
Gsalles10-May-11 9:42 
AnswerRe: How can I access a method on a child page from its master page ? Pin
Not Active10-May-11 10:22
mentorNot Active10-May-11 10:22 
GeneralRe: How can I access a method on a child page from its master page ? Pin
AspDotNetDev10-May-11 10:56
protectorAspDotNetDev10-May-11 10:56 
GeneralRe: How can I access a method on a child page from its master page ? Pin
Not Active10-May-11 11:31
mentorNot Active10-May-11 11:31 
GeneralRe: How can I access a method on a child page from its master page ? Pin
AspDotNetDev10-May-11 13:23
protectorAspDotNetDev10-May-11 13:23 
GeneralRe: How can I access a method on a child page from its master page ? Pin
Not Active10-May-11 13:43
mentorNot Active10-May-11 13:43 
GeneralRe: How can I access a method on a child page from its master page ? Pin
AspDotNetDev10-May-11 16:20
protectorAspDotNetDev10-May-11 16:20 
AnswerRe: How can I access a method on a child page from its master page ? Pin
AspDotNetDev10-May-11 10:55
protectorAspDotNetDev10-May-11 10:55 
I will assume you are using C#. Use this.Page.

First, you will create an interface that defines the methods you want to call (I put this code in INamedObject.cs):
C#
namespace WebApplication70
{
    public interface INamedObject
    {
        string GetName();
    }
}

Then, your Primary.Master will access this.Page and cast it as an INamedObject to call GetName():
ASP.NET
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Primary.master.cs" Inherits="WebApplication70.Primary" %>
<%@ Import Namespace="WebApplication70" %>

<html>
<head runat="server">
    <title>Master/Page Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1><asp:Label runat="server" ID="lblPageName" /></h1>
        <asp:ContentPlaceHolder ID="body" runat="server" />
    </div>
    </form>
</body>
</html>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
        {
            string pageName;
            if (this.Page is INamedObject)
            {
                pageName = (this.Page as INamedObject).GetName();
            }
            else
            {
                pageName = "Unknown Page";
            }
            lblPageName.Text = pageName;
        }
</script>


Create a page, Default.aspx, that makes use of the masterpage:
ASP.NET
<%@ Page Language="C#" MasterPageFile="~/Primary.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication70.Default" %>

<asp:Content ContentPlaceHolderID="body" runat="server">
    <p>This is the default page.</p>
</asp:Content>


In the code behind (Default.aspx.cs), you will implement the interface:
C#
namespace WebApplication70
{
    public partial class Default : System.Web.UI.Page, INamedObject
    {
        public string GetName()
        {
            return "Default.aspx";
        }
    }
}


GeneralRe: How can I access a method on a child page from its master page ? Pin
Keith Barrow10-May-11 11:40
professionalKeith Barrow10-May-11 11:40 
GeneralRe: How can I access a method on a child page from its master page ? Pin
AspDotNetDev10-May-11 12:02
protectorAspDotNetDev10-May-11 12:02 
GeneralRe: How can I access a method on a child page from its master page ? Pin
Keith Barrow10-May-11 12:06
professionalKeith Barrow10-May-11 12:06 
GeneralRe: How can I access a method on a child page from its master page ? Pin
AspDotNetDev10-May-11 12:09
protectorAspDotNetDev10-May-11 12:09 
GeneralRe: How can I access a method on a child page from its master page ? [modified] Pin
Keith Barrow10-May-11 12:18
professionalKeith Barrow10-May-11 12:18 
Questionfetching radiobutton control value from gridview Pin
Karan_TN9-May-11 22:52
Karan_TN9-May-11 22:52 
AnswerRe: fetching radiobutton control value from gridview Pin
Roman_wolf10-May-11 0:29
Roman_wolf10-May-11 0:29 
GeneralRe: fetching radiobutton control value from gridview Pin
Karan_TN10-May-11 1:33
Karan_TN10-May-11 1:33 
GeneralRe: fetching radiobutton control value from gridview Pin
Roman_wolf10-May-11 20:58
Roman_wolf10-May-11 20:58 
QuestionA stupid CustomValidator [Solved] Pin
Ali Al Omairi(Abu AlHassan)9-May-11 22:12
professionalAli Al Omairi(Abu AlHassan)9-May-11 22:12 
AnswerRe: A stupid CustomValidator [Solved] Pin
Ali Al Omairi(Abu AlHassan)10-May-11 3:30
professionalAli Al Omairi(Abu AlHassan)10-May-11 3:30 
Questionhow can add javascript code ?? Pin
lolostar9-May-11 14:41
lolostar9-May-11 14:41 
AnswerRe: how can add javascript code ?? Pin
Anurag Gandhi9-May-11 19:13
professionalAnurag Gandhi9-May-11 19:13 
AnswerRe: how can add javascript code ?? Pin
Nitin S9-May-11 19:19
professionalNitin S9-May-11 19:19 
GeneralRe: how can add javascript code ?? Pin
lolostar9-May-11 20:10
lolostar9-May-11 20:10 
GeneralRe: how can add javascript code ?? Pin
Nitin S9-May-11 20:13
professionalNitin S9-May-11 20:13 
GeneralRe: how can add javascript code ?? Pin
Ali Al Omairi(Abu AlHassan)9-May-11 22:08
professionalAli Al Omairi(Abu AlHassan)9-May-11 22:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.