interviews.dotnetthread.com

↑ Grab this Headline Animator

Sunday, November 9, 2008

How to access Master page members(like properties) from child page.

To Access public members like Property of a Master Page in Child Page we need to specify @MasterType directive with virtualpath attribute pointing to MasterPage path. i.e Use the @MasterType directive to set the strong type for the master page, as accessed through the Master property.

MasterPage:
public string masterVariable = "interviews.dotnetthread.com".
....

Child Page:

<%@ Page Language="C#" MasterPageFile="~/master/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="menu_master_Default" Title="Untitled Page" %>
<%@ MasterType VirtualPath="~/master/MasterPage.master" %>

Then in Page_Load we can access as below
sting childVar = this.Master.masterVariable;

Other Tips:
But If we want to access master page control in child page we can access using Master.FindControl() method.

If we want to access Child Page controls in Master Page then
ContentPlaceHolder1.FindControl() method of contentplaceholder is used.

Happy Coding...

Submit this story to DotNetKicks

No comments:

Post a Comment

Post your comments/questions/feedback for this Article.

 

Latest Articles