access master page from ascx control - asp.net

I have a public property in a master page. I want to access this property from an ascx user control.
My master page is named master_public and it's in a namespace called "master".
So I tried writing:
dim m=ctype(page.master,master.master_public)
dim foobar=m.foobar
The intellisense says that master.master_public doesn't exist.
I tried removing the namespace from the master page. Still no luck.
The master page is declared "partial public class". But this name doesn't seem to be recognized.
Answers here Accessing Master page control in ascx file seem to imply that this should just ... work. Is there some attribute or setting or something I have to include to make master pages accessible as class types?

Add the following on top of your form (.aspx):
<%# MasterType TypeName="master.master_public" %>
The above directive will expose public members of the masterpage to the form. To access your property from the form, simply reference as below:
Me.Master.YourProperty
Therefore, in order to access the masterpage public property from user controls added to the form, just cast the master page object :
CType(Me.Page.Master, master.master_public).YourProperty

Related

Master page inheritence dilemma

Bit of a confusion for me here. This is ASP.NET 4.0. Looking at the markup, it appears that Default.aspx inherits from Site.Master page. But looking at the class definition of Default.aspx (which is named _Default), it inherits from Page class and not SiteMaster.
Now I need to share a few functions across multiple inherited pages and was looking to add them to SiteMaster class so that they would be available in all inherited pages. Can I use SiteMaster class for my purpose, or should I add an independent module to my project and add all my functions to that?
Figured it out. For anyone else looking for a solution, all you need to do is to add the following directive to your markup:
<%# MasterType VirtualPath="~/Site.Master" %>
You can add this line to the top of ASPX file, just under the <%# Page ... %> line. Once added, simply rebuild your project. Now all the public members of your master page class will become available through the strongly-typed member named Master. For example, you can now use the following code to access a public function called MyFunc() defined in the master page class:
this.Master.MyFunc(); //C#
Me.Master.MyFunc() 'VB.NET
MasterType directive actually adds a shadowing member named Master that is strongly-typed as SiteMaster class (or whatever master page class you have specified) when spitting the code-behind class. This new member hides the base class member (whose type is the generic MasterPage class) and thus allows you to access your master page members directly. The property is defined in the .aspx.Designer.cs/vb file like this:
Public Shadows ReadOnly Property Master() As YourProjectNamespace.SiteMaster
Get
Return CType(MyBase.Master, TagTrakkerOnline2.SiteMaster)
End Get
End Property

ASP.NET User Control instance is null when referenced on Page_Load on page code behind

I have a user control that I have written and have added to an ASP.NET page, and functions just fine. However, I am trying to reference a property in the that custom control from code behind, on Page_Load, but cannot, because the variable, which is accessible, for the instance is null.
Is this normal for user controls and Page_Load? And if so, how can I make a reference to the control's instance in order to access its public properties? This is something I need to do before the page is rendered, in order to initialize some variables.
I had the same issue, and it turned out that I was registering my custom control incorrectly.
Correct Definition:
<%# Register Src="PeriodControl.ascx" TagName="PeriodControl" TagPrefix="ucs" %>
Incorrect Definition:
<%# Register TagPrefix="ucs" Namespace="MyWebsite" Assembly="MyWebsite" %>
The only difference was to reference the ascx file directly instead of the control in the assembly. Go figure!?
You can probably access your user control from the Page_PreRender event.
You can find more documentation about the page life cycle in asp.net here.

How does one get a code-behind file to recognize type of a user control?

I have a custom control in my mater page that represents a menu, let's call it CustomMenu. The control code files are located in a non special directory.
I want to give each page that uses that master page the ability to access the control and, using a public property, tell the control whether or not it should render itself.
I have a public property on the control to do that, and I know I can get the control by referencing Page.Master.FindControl('IdOfControlIwant');
The problem I'm having is that I can't seem to get the control Type recognized by the compiler, so when I do find the menu control, I can't actually work with it. The only way I can get the code behind to recognize the type is to register it in the ascx file, and then add at least one control to the page, which is undesirable.
Thoughts?
You have to combine what both Jacob and dzendras have posted. Add the MasterType directive to your content page's aspx file:
<%# MasterType VirtualPath="~/your.master" %>
And in the master page create a property:
public CustomMenu MyCustomMenu {get{ return myCustomMenu;}}
Where myCustomeMenu is the ID of the Usercontrol in your masterpage.
You should now be able to reference the usercontrol from a content page. So, if the CustomMenu usercontrol had a property called SelectedItem, you should be able to access it like this:
public void Page_Load(object o, EventArgs e)
{
Master.MyCustomMenu.SelectedItem = 1;
}
Use the MasterType directive in your pages:
<%# MasterType VirtualPath="~/your.master" %>
That will strongly type your Master page reference, so you should be able to add properties that can be accessed by the pages.
Make a property of your MasterPage class:
bool IsCustomMenuVisible {set{ CustomMenu.Visible = value;}}
And use it wherever you like.

ASP.NET what is the meaning of AutoEventWireup and Inherits?

Given the following statement,
<%# Page Language="C#" AutoEventWireup="true" CodeFile="XXX.aspx.cs" Inherits="XXX" %>
What is the meaning of AutoEventWireup?
What if the value of AutoEventWireup is equal to false
What is the meaning of XXX in the Inherits attribute?
I cannot find the definition of XXX in the auto-created file in ASP.NET 2008. Where is the XXX defined?
Thank you
AutoEventWireup = false means that your Page_Load event will not be automatically hooked to the page's Load event and so on for PreRender and the other page lifecycle events. It means in the constructor of your code-behind base class for the Page, you will have to manually do
Load += new ..EventHandler(Page_Load) etc
Inherits tells the page which class is the base class for the class that the runtime will generate when your application starts up. The auto-generated class will be in the ASP namespace and be put in the Temporary ASP.NET Files and will inherit from your class. This is how protected properties and event declarations in your code-behind can actually serve as handlers that are specified in the declarative .aspx markup
XXX is usually side-by-side right next to the aspx file and is the same name as the aspx file, unless it is Default, in which case that is a C# keyword, so sometimes it uses _default as the class name while the page itself is Default.aspx.
You should probably try to read some tutorials on ASP.NET page inheritance, here is an example, but you should search for more:
http://west-wind.com/weblog/posts/3016.aspx

How do you cast from Page.Master to a specific master page in ASP.NET

I have a BasePage which inherits from System.Web.UI.Page, and every page that inherits the BasePage will have the same master page.
How do I cast the Page.Master of the BasePage to the specific master page so I can access properties on it?
A better way is to add the MasterType property to the pages that use that master. Then you can simply access the master page properties through the page object.
<%# MasterType VirtualPath="~/site.master" %>
You just use this in your code:
this.Master.propertyName
To access the property of the master page for the current page.
Overriden Master can't be done (its not Virtual), and masking it with new causes an issue with the page class not being able to get its master, so the best thing to do is a second property.
Something like:
public CustomMasterPage MasterPage
{
get { return this.Master as CustomMasterPage; }
}
In your BasePage class.
In VB.Net
MasterPageVariable = Ctype(page.MasterPage, MasterPageClass)

Resources