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

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.

Related

access master page from ascx control

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

event of asp.net master page not firing

I put a break point at the protected void Page_Load(object sender, EventArgs e) method of my master page, but when i start the site it does not hit that break point.
Why is the event not firing? I would like to use this event along with others such as the Init event in order to check to see if the session has expired everytime a page loads...
Thanks.
You can check that AutoEventWireup is set to true in the Master declaration.
<%# Master Language="C#" MasterPageFile="~/MasterPages/Main.master" AutoEventWireup="true" CodeFile="Main.master.cs" Inherits="MasterPages_Main" %>
If it is set to false you have to manually connect the events.
The problem is likely that your .aspx page has not correctly referencing your .master page. Be sure that, at the top of your .aspx page, you have a line similar to the following:
<%# Page Title="Some Title" Language="C#" MasterPageFile="Main.Master" CodeBehind="MyPage.aspx.cs" Inherits="MyApp.MyPage" %>
Another possible problem is that your .master page isn't referencing the proper (or any) assembly. Be sure that the top line of your .master page is similar to the following:
<%# Master Language="C#" AutoEventWireup="True" CodeBehind="Main.master.cs" Inherits="MyApp.Main" %>
A couple of things to check, some of which may be obvious...
Check your child page is calling the correct Master Page.
The Master Page Page_Load executes after the child Page_Load, so make sure you debug through the child page execution first.
Check that you've actually got your Page_Load event wired up if you're using VB.NET.
You might want to try creating a base class of type Page that handles your session check. Leave the master pages for page design. If you have multiple master pages, you would have to duplicate that code in each one, but if your pages inherit from a single base page, your session check logic will be in one place.
I had the same problem - the pageload was firing before but something went wrong and it stopped.
What fixed it was putting the page_load event in the .master file not the .master.cs
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//put your code here
//any function u wanna call declare it in the code file as public
}
</script>
I had a slightly different problem and different solution.
Just in case anyone has similar situation as me.
I had a nested master page and the control and related event method were in the "middle" master. The methods did NOT get called when they were placed in .cs file for middle master page. But they got called when included in .master page within script tags as described above by "petra".
This seems to be more of a bug in .net platform - Also - I do not think some of the above complicated solutions are (or should be) needed (e.g., keeping code out of master page and using master page only for structure etc.) - that is more of a workaround and I suspect there indeed is a bug in .net platform with respect to master page event firings (specially with nested master pages as in my case).
You need to check declaration of page to make sure it refers proper masterpage and masterpage, to make sure that it refers proper inherited class.
My error occured because of comment line >>> base.OnLoad(e); in Site.Master.cs
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
PEACE

Access body element from content page via a nested master page

All I want to do is access the <body> element from the code-behind of a content page and add a class name to it.
I have a top-level master page with the <body> element in it. Then I have a nested master page which is the master page for the content page. From the code behind of the content page I want to add a class name to the body element. That's all.
I have this in the top-level master:
<body id="bodyNode" runat="server">
I added this to the code-behind for the content page:
Master.bodyNode.Attributes.add("class", "home-page");
And I get a message that:
System.Web.UI.MasterPage' does not contain a definition for 'bodyNode
If I add this to the aspx content page:
<% # MasterType VirtualPath="~/MasterPage.master"%>
The message then changes to:
bodyNode is inaccessible due to its protection level
Please advise, I've wasted like 2 hours on what feels like something that should be really simple to do :(
once you have set runat="server" for your body node, you have to access it using the HTMLControls namespace. try this.
public void Page_Load(Object sender, EventArgs e)
{
//Inject onload and unload
HtmlGenericControl body = (HtmlGenericControl)Master.FindControl("bodyNode");
body.Attributes.Add("class", "home-page");
}
EDIT
Your problem is that you have nested master pages.
Since the "body" tag is in your top level master page, Master.FindControl() won't work, as that is looking in the nested master page.
What you need to do is use Master.Master.FindControl(), or recursively loop through your master pages, going up until Master.Master is null (as then you know you are at the top level master page) and then calling FindControl() on that.
I would add a public property to the code behind of the master page that would allow access to the body tag that is part of the master page. And then call that property from the content page.

How to access an enum in master page

Can we access an enum which has been defined in the master page. I read about the master page on my own blog.
If you mean an actual enum, defining it elsewhere is a better option, but the same casting below works to get at it as well.
If you want to access the property that is an enum then cast the Master property of your page to your master page's type. Like this:
protected void override OnLoad(EventArgs e)
{
((MyMasterPagesType)Master).MyEnumProperty = MyEnum.Value;
}
Edit:
Almost forgot, you can also have the Master property on your page already behave as this type by using the #MasterType directive in your aspx markup.
<%# MasterType VirtualPath="~/masters/SourcePage.master”" %>

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