Accessing ASP Master Page Properties - asp.net

I have an ASP.net page which uses a master page. The master page has a public property. Does anybody know how can I access the given property from a content page that uses the master page?

Look into Strongly Typing your Master Page link
Check out the <%# MasterType virtualPath="~/MasterPage.master"%> tag.
This enables you to directly have access to the public properties/methods.

Related

Dynamically access master pages properties

We can change masterpagefile for particular page dynamically.but how can i access their properies which are changing as per the master pages.
what code will give me their properties.
Thanks
You can add a public property to the code behind of your master page, like so:
public string MyMasterPageProperty {
return "my stuff";
}
Then, on the content page, you can use this code to access your new property.
Page.Master.MyMasterPageProperty;
If you don't want to add the MasterType directive on the aspx page, you can always explicitly cast the Page.Master to the type of your master page, so that it looks like this:
((MyMasterPageClass)Page.Master).MyMasterPageProperty;
You can read more info here:
https://web.archive.org/web/20210513005959/https://www.4guysfromrolla.com/articles/013107-1.aspx
You'll need the MasterType directive on the aspx page:
<%# MasterType VirtualPath="~/Site1.Master" %>
Then on your aspx.cs page, you can call the properties of the master:
Master.Property1 = "whatever you wanna do";
If you need more instructions:
http://dotnet.dzone.com/news/back-basics-%E2%80%93-using-mastertype

how to apply master page to pages in asp.net 2008

I am using .net 2008 for developing web application.
So I created a master page and some other pages. I want to apply master page to all other pages but I'm not getting it. Earlier i use .net2005 here there is option when we created a new page in application and check the master page option, but in .net2008 this option is not there so please tell me how to do it. I'm new to .net 2008 .
Please tell me
Thank you
If you want to manually set the masterpage for pages you have already created you can do the following: In the Page directive of your ASPX pages you set the masterpage like this:
<%# Page Language="C#" MasterPageFile="~/MasterPages/Master1.master" Title="Content Page"%>
Your MasterPage defines a few Content areas. You then should reuse those in your page and fill them with the local content.
When adding a new page the documentation says that when adding a Web Forms page you can select the Select master page check box, and then click Add. Then the Select a Master Page dialog box should appear.
You indicating that in your question
"when we created a new page in application and check the master page option"
If you are using a web application when you wan't to add a page with masterpage
you should add the page like this
Project ->
Add New Item ->
Web Form Using Master Page (instead of selecting Web Form)
Then you will have the option to select the masterpage
Use this code:
<%# Page Title="test" Language="VB" MasterPageFile="~/MasterPageName.master" AutoEventWireup="false" CodeFile="PageName.aspx.vb" Inherits="PageNameClass" %>
<asp:Content ID="Content1" ContentPlaceHolderID="maincontent" runat="Server">
//here you can add you page data start from form tag till end of form tag and remove everything else.
</asp:Content>

ASP.NET #Register vs. #Reference

I'm working with referencing user controls on my ASPX page and I'm wondering what the difference is between these two page directives.
#Reference
#Register
#Register is primarily used for registering tag prefixes to declaratively use controls within a page.
<%# Register tagprefix="my" namespace="MyNamespace" %>
<my:CustomControl runat=server />
#Reference is primarily used to refer to a page or user control (by file name or virtual path) to programatically refer to members of the page or control.
<%# Reference Control="MyControl.ascx" %>
<% MyControl ctrl = (MyControl) Page.LoadControl("MyControl.ascx");
ctrl.CustomProperty = "..."; //REFERENCE directive is needed to access property
%>
#Register is the more commonly used directive. You use this when you want to use a user control in your aspx or ascx page declaratively. #Register associates the control with a specific prefix and you can then use it in your markup.
#Reference only tells ASP.NET to compile the other control when your aspx or ascx page is compiled. That makes sure it is available at run-time and can be added to your control hierarchy programmatically. This is less common since dynamically changing user controls at runtime is not comon.
Here's a good blog post about it.
http://weblogs.asp.net/johnkatsiotis/archive/2008/08/13/the-reference-directive.aspx

Asp.net jquery post to ascx: This type of page is not served

When I'm trying to call an ascx with Jquery post I get:
"This type of page is not served"
I think it's something to do with the IIS not allowing calls directly to ascx.
Is it possible to allow posting to ascx?
I have IIS6.
Thanks.
No, .ascx is a control, it is included and rendered in .aspx pages.
So the error is correct, ascx should not be served.
Wrap it in an aspx page like so:
<%# Page %>
<%# Register TagPrefix="scott" TagName="header" Src="Controls/Header.ascx" %>
<scott:Header runat=server id="control1" />
Just FYI, in an MVC application, you are actually allowed to load an .ascx by pointing the URL to an Action that returns a PartialView.

How to access controls inside a nested master page? why does it behave differently from content pages?

Is there a difference between these two scenarios:
(1) Accessing a property on a master page from a regular child
(2) Accessing a property on a master page from a nested master page
I tried to access a textbox in the master page from a content page like this:
TextBox a;
a = (TextBox)Master.FindControl("ayyash"); // Master is declared in MasterType directive
defaultTextbox.Text = a.Text; // defaultTextBox is a textbox control inside default.aspx
it works, but then when I applied the same method on a nested master page:
TextBox a;
a = (TextBox)Master.FindControl("ayyash"); // Master is declared in MasterType directive
myTextBox.Text = a.Text; // myTextBox is a textbox control inside child.master
this does not work, am I missing something? I call both codes inside regulare page_load handler...
I also noticed I cannot set textbox value inside the nested master page from code behind, there is definitely something im missing, what is it?
To shed light on this issue, here is an example:
Nested Master Page:
<%# Master Language="C#" MasterPageFile="MasterPage.master" AutoEventWireup="false" CodeFile="MasterPage2.master.cs" Inherits="MasterPage2" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:textbox id="tx2" runat="server" text="this is two"></asp:textbox>
<asp:contentplaceholder id="newstuff" runat="server"></asp:contentplaceholder>
</asp:Content>
Code behind:
Response.Wrote(tx2.Text);
I get NOTHING, why what did I miss? note that I also tried the recursive find control:
String str = ((TextBox)((Content)FindControl("Content2")).FindControl("tx2")).Text;
still nothing
ContentPlaceHolder cp = (ContentPlaceHolder)this.Master.Master.FindControl("ContentPlaceHolder1");
//base content place holder id
Label objLabel3 = (Label)cp.FindControl("lblNested");
//lblNested is id in nested master page
I read few things here:
http://www.odetocode.com/Articles/450.aspx
and found out that the nested page in the middle never calls Page_Load! instead, it fires a load event that you can catch to set whatever fields, so the answer was in: on nested page do the following:
protected override void OnLoad(EventArgs e)
{
myTextBox.Text = "anything";
base.OnLoad(e);
}
This should work without any problems, so something else must be wrong. I just tried it inside a simple test project and I have no problems finding a control on the master page in both cases.
I would check (again) if you refer to the correct master page inside your nested master page. What you could also check is the runtime type of the Master property inside your nested master page. It should be the type of your master page.
EDIT: I thought the issue was about finding a control in the root master page from a nested master page and this should work without any problems. For finding a control inside a content placeholder in a nested master page, take a look at the following forum post.
You can have absolute control of your content in both master page and nested page from your content page using the directives:
<%# MasterType VirtualPath="your_master.master" %>
<%# Reference VirtualPath="~/your_master.master" %>
See the excellent article from K. Scott Allen in Ode To Code

Resources