473,545 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MasterPage -> Page -> MasterPage Code Access

Hi. I'm a little confused about the code that resides in the code-behind of a
MasterPage and the code that resides in the code-behind of the actual pages
that USE that MasterPage.

I'm noticing, for example, that the Page_Load on the specific page executes
before the Page_Load of its MasterPage. Is this right? But what I really want
to understand is VARIABLE SCOPE, etc. between the two.

For example: Is there a way for me to write code in the MasterPage which
will access the objects, tags, etc. of the specific Pages that USE that
MasterPage? So, can I write a function that lives with the MasterPage that
will look into the specific page at load time and do things based on the
tags, etc. on that page? If so, how?

Alex
Apr 10 '06 #1
3 2074
You can access controls in the master page from the child page using the
master.FindCont rol("controlID" ); syntax. But you cannot access anything at
all in the child page from the master page. If you want something to happen
in the master page, depending on which child page is using it, put your code
in the relevent child page and use the Master.FindCont rol("controlID" );
syntax outlined above. You can also expose controls and vairables etc of the
master page as properties.

"Alex Maghen" wrote:
Hi. I'm a little confused about the code that resides in the code-behind of a
MasterPage and the code that resides in the code-behind of the actual pages
that USE that MasterPage.

I'm noticing, for example, that the Page_Load on the specific page executes
before the Page_Load of its MasterPage. Is this right? But what I really want
to understand is VARIABLE SCOPE, etc. between the two.

For example: Is there a way for me to write code in the MasterPage which
will access the objects, tags, etc. of the specific Pages that USE that
MasterPage? So, can I write a function that lives with the MasterPage that
will look into the specific page at load time and do things based on the
tags, etc. on that page? If so, how?

Alex

Apr 10 '06 #2
Thanks for Clickon's inputs,

Hi Alex,

As for the ASP.NET 2.0's MasterPage, it is dynamically loaded into the
concrete page which has been configured to apply that masterpage. You can
think the masterpage like a Usercontrol which loaded into the page's
control collection, the relationship is different from the class
inheritance in OOP. Also, for your requirement on accessing object, data
in concrete page through the MasterPage's codebehind code, I think it is
certainly possible. Clickon has mentioned the means that we can query the
controls through the FindControl method on page. And actually the "Page"
property of the MasterPage reference to the instance of the concrete
page(you can use the Page.GetType to verify this). So since we can get the
concrete page instance reference in MasterPage, we can access public
methods, properties (built-in or custom ones) in our master page's code.
One problem remains is the Type Casting, since the Page property is of the
System.Web.UI.P age class, if we want to access any custom property or
methods in the concrete page class, we need to do the casting. However,
it's not very convenient (and not good practice) to hardcoded concrete
page's type and casting it. One good approach is defining a common
interface which expose some properties or functions which will be useful to
the MasterPage, then our concrete page can implement this interface and
according to the detailed page structure or info. e.g:

========in master page==========
public partial class App_Master_simp le : System.Web.UI.M asterPage
{
protected void Page_Load(objec t sender, EventArgs e)
{
Response.Write( "<br/>" + ((IHello)Page). SayHello("Steve n"));
}
}
========the concrete page which applying the master page=====

public partial class concretes_newCo ncretePage : System.Web.UI.P age, IHello
{
............... ............... ...

#region IHello Members

public string SayHello(string name)
{
return "Hello " + name + "<br/>" + TextBox1.Text;
}

#endregion
}
We can expose control info or other custom data/properties or operations
in concrete page to master page through such interface.

Hope this helps. If there is any other ideas or questions, please feel free
to post here.

Regards,

Steven Cheng
Microsoft Online Community Support
=============== =============== =============== =====

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Apr 11 '06 #3
Hi Alex,

Does my further suggestion help a little? If there's anything else we can
help, please feel free to post here.

Regards,

Steven Cheng
Microsoft Online Community Support
=============== =============== =============== =====

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.
Apr 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1277
by: Anthony Christianson | last post by:
Here is the Scenario: I have a web service that performs processing on data. I need to call this web service from an ASP page. I created a ServicedComponent that has the WebService Proxy and provides a COM Interface to the ASP code to make requests to the Web Service. ASP Code: dim objWsProxy, strReturn set objWsProxy =...
0
1975
by: Brian Loesgen | last post by:
The next San Diego .Net User Group meeting is Tuesday, November 25, 2003 at the Scripps Ranch Library. Scripps Ranch Library 10301 Scripps Lake Drive San Diego, CA 92131-1026 Please join us at 6:00 pm for pizza and networking. The meeting starts at 6:30 pm.
1
2343
by: Namratha Shah \(Nasha\) | last post by:
Hey Guys, Before we start with our sample app we need to view the security configuration files on the machine. You will find them under <drive>\WInNT\Microsoft.NET\FrameWork\<version>\Config Enterprise Level Security configuration file is :- enterprise.config Machine Level Security configuration file is :- security.config
1
1570
by: Ramzey | last post by:
I've looked through the MSDN documentation on code access security and can not seem to find an answer to my question. I have a class XYZCorpWebPage that uses System.Web.UI.Page as it's base class. I want to make it so that the web server will throw an exception/deny runtime rights to any WebForm derived from System.Web.UI.Page, but I want...
1
1524
by: RA | last post by:
How can I have a resticted access to assembly? I want onlt code I am writing to be able to access my assemblys. Thanks, Ron
0
2079
by: Namratha Shah \(Nasha\) | last post by:
Hey Guys, Today we are going to look at Code Access Security. Code access security is a feature of .NET that manages code depending on its trust level. If the CLS trusts the code enough to allow it ro run then it will execute, the code execution depends on the permission provided to the assembly. If the code is not trusted wnough to run...
1
1522
by: Spyros Sakellariadis | last post by:
I have a standard aspx page format that I replicate in many places on my site. That page has a lengthy page_load section, that I'd love to stick in a single code file (e.g. MyFunctions.vb). I know how to do this for simple functions such where I pass in and get back string parameters etc, but the code I want to stick in that code file includes...
4
1717
by: Hugh O | last post by:
Hi, I have been able to use the following kinds of code to access basic information on the individual controls that would be in any Web page. ?Me.Controls(1).Controls(56).GetType.Name ' control type ?Me.Controls(1).Controls(56).ID ' control name Is there a similar way of getting code access to the properties...
1
1004
by: CMM | last post by:
Searched and searched... couldn't find an answer to this... how does one determine the DOCTYPE tag of a page from the code-behind? Basically, I'm in a situation where I need to output different html from proc in the code-behind based on the declared doctype in the HTML source. -- -C. Moya www.cmoya.com
0
7478
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7410
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5984
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4960
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.