473,520 Members | 2,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cannot access strongly typed properties in Master page

I am trying to access strongly typed properties in a page's master page from
the content page. I followed an article that describes how to do this, and
it seems quite straight forward. However when I try to access the property
from my content page intellisense does not show the public property.

Likewise when I go to compile I get an error saying that

Specifically here is the article I am trying to follow:

http://msdn.microsoft.com/library/de...asterpages.asp

It says "... there is a much easier and more object-oriented approach. Since
the Master property is strongly typed any public properties exposed by the
MasterPage class, including those you create, can be early-bound. So if I
create two string properties, Title and Keywords, along with their
associated data members in the code-behind file:

protected String m_HtmlTitle =
"http://www.dotnetdashboard.net";

public String HtmlTitle
{
get { return m_HtmlTitle; }
set { m_HtmlTitle = value; }

I put that above code in the Code Behind page in my Master page. Then I
created a new web form and pointed at that master page as the pages master.
In this new web form's Page_OnLoad I put this code:
this.Master.HtmlTitle = "This is a test";

But when I press the "." after "Master" I do not get any Intellisense option
for "HtmlTitle". Likewise when I compile I get the following error:
Error 2 'System.Web.UI.MasterPage' does not contain a definition for
'HtmlTitle' D:\mytest\test.aspx.cs

If I take that line out the page does load, and its definately pulling in
content from that master page just fine.

I realize there are other ways to override the title. However I would like
to use this technique of adding strongly typed public properties for other
purposes. So I'd like to figure out why it is not working. I think I am
following the example but must be missing something.

Does anyone know what I may be doing wrong or how to get this to work as the
article describes?

** UPDATE: Just before I sent this message it occurred to me that perhaps I
needed some sort of cast. My master page is called MyTemplate and I noticed
that in MyTemplate its defined as a class which derrives from MasterPage.

So I decided to try this, and THIS WORKS!
((MyTemplate)this.Master).HtmlTitle = "THIS IS A TEST";

However - this is quite awkward to write, and the articles I read do not
mention the need to do this. So my questions are:

1) Is using a cast like this really the correct way to do this?

2) Is this a more elegant way or a way that does not require an explict cast
like this? It seems odd that this would be the right way, especially given
that no articles on the subject talk about the need to do this.

Thanks,

Steve
Nov 19 '05 #1
2 4165
In digging around more I may have discovered the answer. Aparrently you
need to add something like:
<%@ MasterType VirtualPath="~/MyTemplate.master" %>

If you want to avoid the cast. Adding that allows me to directly access
this.Master.HtmlTitle.

Does anyone know if there is a way to globally specific that MasterType so
that I do not have to repeat it on every page?

Steve
Nov 19 '05 #2

Hi Steve:

Yes, you can use the MasterType property.
http://odetocode.com/Blogs/scott/arc...7/16/1944.aspx

Although you can set the masterPageFile globally in web.config I don't
believe you can set MasterType there, unfortunately.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Sun, 18 Sep 2005 23:20:20 -0400, "Steve Franks"
<pl****@postreplyhere.com> wrote:
I am trying to access strongly typed properties in a page's master page from
the content page. I followed an article that describes how to do this, and
it seems quite straight forward. However when I try to access the property
from my content page intellisense does not show the public property.

Likewise when I go to compile I get an error saying that

Specifically here is the article I am trying to follow:

http://msdn.microsoft.com/library/de...asterpages.asp

It says "... there is a much easier and more object-oriented approach. Since
the Master property is strongly typed any public properties exposed by the
MasterPage class, including those you create, can be early-bound. So if I
create two string properties, Title and Keywords, along with their
associated data members in the code-behind file:

protected String m_HtmlTitle =
"http://www.dotnetdashboard.net";

public String HtmlTitle
{
get { return m_HtmlTitle; }
set { m_HtmlTitle = value; }

I put that above code in the Code Behind page in my Master page. Then I
created a new web form and pointed at that master page as the pages master.
In this new web form's Page_OnLoad I put this code:
this.Master.HtmlTitle = "This is a test";

But when I press the "." after "Master" I do not get any Intellisense option
for "HtmlTitle". Likewise when I compile I get the following error:
Error 2 'System.Web.UI.MasterPage' does not contain a definition for
'HtmlTitle' D:\mytest\test.aspx.cs

If I take that line out the page does load, and its definately pulling in
content from that master page just fine.

I realize there are other ways to override the title. However I would like
to use this technique of adding strongly typed public properties for other
purposes. So I'd like to figure out why it is not working. I think I am
following the example but must be missing something.

Does anyone know what I may be doing wrong or how to get this to work as the
article describes?

** UPDATE: Just before I sent this message it occurred to me that perhaps I
needed some sort of cast. My master page is called MyTemplate and I noticed
that in MyTemplate its defined as a class which derrives from MasterPage.

So I decided to try this, and THIS WORKS!
((MyTemplate)this.Master).HtmlTitle = "THIS IS A TEST";

However - this is quite awkward to write, and the articles I read do not
mention the need to do this. So my questions are:

1) Is using a cast like this really the correct way to do this?

2) Is this a more elegant way or a way that does not require an explict cast
like this? It seems odd that this would be the right way, especially given
that no articles on the subject talk about the need to do this.

Thanks,

Steve


Nov 19 '05 #3

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

Similar topics

1
3287
by: phil1bruening | last post by:
Hello! I need to access master page controls from within content pages. Wow can I do that??
4
6004
by: RedHair | last post by:
I have a base page class A which inherits System.Web.UI.Page class then a aspx page B inherits class A and have a master page C. There is a property D (a string variable) in the master page C. How can I access the property D from the base page class A? PS: I know how to use reflection to access control of master page C from the base class...
4
3960
by: Boris Yeltsin | last post by:
OK, on my Master Page I have a control: <a id="hypTabAccount" href="#" runat="server">Account</a> Now, in the code-behind (Root.master.vb) I can refer to it simply thus: hypTabAccount.InnerText = "blah" Now, what I want is the same in a content page that uses the Master Page. I have a Master Type in my Content page:
4
3730
by: Nick Gilbert | last post by:
Hi, Is it possible to access the Session of an arbitary user from an aspx page? On an e-commerce site, I am notified of payment success via a callback from the payment server to an ASPX page on my system. I would like to be able to access the session of the user that submitted the order, and clear their basket. I don't really want to...
12
2079
by: =?Utf-8?B?Smlt?= | last post by:
I have code that reads and parses a text file using a schema.ini file. Works great. When I see the dataGrid it's exactly what I want. Dim CString As String = _ "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\;" & _ "Extended Properties=""text;HDR=No;""" Dim TConnect As New System.Data.OleDb.OleDbConnection(CString)
7
16621
by: groups | last post by:
This is my first foray into writing a generic method and maybe I've bitten off more than I can chew. My intent is to have a generic method that accepts a value name and that value will be returned from the source. My first attempt was as follows; (please ignore that error handling is not present in this example) public T...
6
9119
by: =?Utf-8?B?SmF5IFBvbmR5?= | last post by:
I am trying to access a Public property on a Master Page from a Base Page. On the content pages I have the MasterType Directive set up as follows: <%@ MasterType virtualpath="~/Master.master" %> On the Master Page I have a public property exposed: Public Property ErrorMessage() As String Get Return txtError.InnerText End Get
4
3092
by: Rachana | last post by:
Hi, I have understood Data Sets but what is meant by typed/untyped/ strongly typed datasets. Can any one explain me or suggest any site/ article, to get these concepts (and their comparisions) cleared? Thanks, Rachana
7
2282
by: Andy B | last post by:
I have a class I am creating for data access. I need to access controls from inside the class that are on a particular page. How do I do this? or is creating an instance of the page class and using FindControl the only way to do it?
0
7204
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...
0
7602
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7164
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
7560
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
5742
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
5127
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
4788
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
3282
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
506
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.