PC Review


Reply
Thread Tools Rate Thread

Accessing a Master Page Property from a Module

 
 
Kirk
Guest
Posts: n/a
 
      24th May 2007
I have been reading Scott Allen's article on Master Pages (http://
odetocode.com/Articles/450.aspx) but I am having problems
understanding a concept. Specifically, I have created a property
(called "CurFlag") on my master page that can be accessed from content
pages, but not from separate code modules.

I tried doing something like this in my VB code module:

Dim myMaster As MasterPage = CType(ProjectMgmt.Master,
MasterPage)
myMaster.CurFlag = "value"

....but this just gets me an "Reference to a non-shared member requires
an object reference" error on the first line. Forgive my ignorance,
but is my syntax correct, or do I need to add something additional to
the master page so that my module "sees" this master page?

I would appreciate any suggestions.
Thank you.

 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      24th May 2007
On May 24, 10:18 pm, Kirk <lok...@hotmail.com> wrote:
> I have been reading Scott Allen's article on Master Pages (http://
> odetocode.com/Articles/450.aspx) but I am having problems
> understanding a concept. Specifically, I have created a property
> (called "CurFlag") on my master page that can be accessed from content
> pages, but not from separate code modules.
>
> I tried doing something like this in my VB code module:
>
> Dim myMaster As MasterPage = CType(ProjectMgmt.Master,
> MasterPage)
> myMaster.CurFlag = "value"
>
> ...but this just gets me an "Reference to a non-shared member requires
> an object reference" error on the first line. Forgive my ignorance,
> but is my syntax correct, or do I need to add something additional to
> the master page so that my module "sees" this master page?
>
> I would appreciate any suggestions.
> Thank you.


The instance must first be declared as an object variable and then
referenced by the variable name.

Dim myMaster As MasterPage = new ProjectMgmt.Master

 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      24th May 2007
On May 24, 10:29 pm, Alexey Smirnov <alexey.smir...@gmail.com> wrote:
> On May 24, 10:18 pm, Kirk <lok...@hotmail.com> wrote:
>
>
>
>
>
> > I have been reading Scott Allen's article on Master Pages (http://
> > odetocode.com/Articles/450.aspx) but I am having problems
> > understanding a concept. Specifically, I have created a property
> > (called "CurFlag") on my master page that can be accessed from content
> > pages, but not from separate code modules.

>
> > I tried doing something like this in my VB code module:

>
> > Dim myMaster As MasterPage = CType(ProjectMgmt.Master,
> > MasterPage)
> > myMaster.CurFlag = "value"

>
> > ...but this just gets me an "Reference to a non-shared member requires
> > an object reference" error on the first line. Forgive my ignorance,
> > but is my syntax correct, or do I need to add something additional to
> > the master page so that my module "sees" this master page?

>
> > I would appreciate any suggestions.
> > Thank you.

>
> The instance must first be declared as an object variable and then
> referenced by the variable name.
>
> Dim myMaster As MasterPage = new ProjectMgmt.Master- Hide quoted text -
>
> - Show quoted text -


Wait... there must be a name of the class

I think, ProjectMgmt.Master is the name of your Master page

 
Reply With Quote
 
=?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=
Guest
Posts: n/a
 
      24th May 2007
Kirk,

Not sure what are you trying to achieve - why would you need to access
master page (which is generally page specific - as different pages may have
different master pages) from a module? You will only want to access a
particular page's master page instance - in this case you will need a
reference to the page instance (or any control on the page) or to the master
itself.

If you need to change your master for all page - do it inside your Master,
and you will have the reference to it.

If you provide a bit more on what you are trying to achieve, it will be
easier to suggest a better way to accomplish the task.

"Kirk" wrote:

> I have been reading Scott Allen's article on Master Pages (http://
> odetocode.com/Articles/450.aspx) but I am having problems
> understanding a concept. Specifically, I have created a property
> (called "CurFlag") on my master page that can be accessed from content
> pages, but not from separate code modules.
>
> I tried doing something like this in my VB code module:
>
> Dim myMaster As MasterPage = CType(ProjectMgmt.Master,
> MasterPage)
> myMaster.CurFlag = "value"
>
> ....but this just gets me an "Reference to a non-shared member requires
> an object reference" error on the first line. Forgive my ignorance,
> but is my syntax correct, or do I need to add something additional to
> the master page so that my module "sees" this master page?
>
> I would appreciate any suggestions.
> Thank you.
>
>

 
Reply With Quote
 
Kirk
Guest
Posts: n/a
 
      25th May 2007
On May 24, 6:26 pm, Sergey Poberezovskiy
<SergeyPoberezovs...@discussions.microsoft.com> wrote:
> Kirk,
>
> Not sure what are you trying to achieve - why would you need to access
> master page (which is generally page specific - as different pages may have
> different master pages) from a module? You will only want to access a
> particular page's master page instance - in this case you will need a
> reference to the page instance (or any control on the page) or to the master
> itself.
>
> If you need to change your master for all page - do it inside your Master,
> and you will have the reference to it.
>
> If you provide a bit more on what you are trying to achieve, it will be
> easier to suggest a better way to accomplish the task.
>
>
>
> "Kirk" wrote:
> > I have been reading Scott Allen's article on Master Pages (http://
> > odetocode.com/Articles/450.aspx) but I am having problems
> > understanding a concept. Specifically, I have created a property
> > (called "CurFlag") on my master page that can be accessed from content
> > pages, but not from separate code modules.

>
> > I tried doing something like this in my VB code module:

>
> > Dim myMaster As MasterPage = CType(ProjectMgmt.Master,
> > MasterPage)
> > myMaster.CurFlag = "value"

>
> > ....but this just gets me an "Reference to a non-shared member requires
> > an object reference" error on the first line. Forgive my ignorance,
> > but is my syntax correct, or do I need to add something additional to
> > the master page so that my module "sees" this master page?

>
> > I would appreciate any suggestions.
> > Thank you.- Hide quoted text -

>
> - Show quoted text -


Thanks for all of the replies. My situation is fairly simple: I have
one master page (called "ProjectMgmt.Master,
MasterPage") that has about 10 content pages.

I have a function in a VB module that does some math/validation for
me. This function is called by a button on a content page. When I
arrive at the result, I want to change a label on the master page (so
it is visible on all pages of the site).

Is there a better way to do what I am trying to accomplish? If so, I
would appreciate any code syntax examples you could give me. I don't
know the "proper" names for some objects, so I am unable to follow the
first reply's suggestion (just a newbie, I guess).

Thank you again.

If there is a better way to do this than

 
Reply With Quote
 
=?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=
Guest
Posts: n/a
 
      25th May 2007
So, you want to only display this label (on all pages) after a certain event
happens on a page - correct? Then store this calculated value (in Session,
for example) and read it on your Master PreRender event (this will ensure
that all button clicks and similar events from other controls have already
been fired and processed).
Something similar to the following:
button click:
Me.Session("MyMasterLabelText") = ModuleName.GetLabelText(...)
Master prerender:
MyLabel.Text = Me.Session("MyMasterLabelText")

"Kirk" wrote:

> On May 24, 6:26 pm, Sergey Poberezovskiy
> <SergeyPoberezovs...@discussions.microsoft.com> wrote:
> > Kirk,
> >
> > Not sure what are you trying to achieve - why would you need to access
> > master page (which is generally page specific - as different pages may have
> > different master pages) from a module? You will only want to access a
> > particular page's master page instance - in this case you will need a
> > reference to the page instance (or any control on the page) or to the master
> > itself.
> >
> > If you need to change your master for all page - do it inside your Master,
> > and you will have the reference to it.
> >
> > If you provide a bit more on what you are trying to achieve, it will be
> > easier to suggest a better way to accomplish the task.
> >
> >
> >
> > "Kirk" wrote:
> > > I have been reading Scott Allen's article on Master Pages (http://
> > > odetocode.com/Articles/450.aspx) but I am having problems
> > > understanding a concept. Specifically, I have created a property
> > > (called "CurFlag") on my master page that can be accessed from content
> > > pages, but not from separate code modules.

> >
> > > I tried doing something like this in my VB code module:

> >
> > > Dim myMaster As MasterPage = CType(ProjectMgmt.Master,
> > > MasterPage)
> > > myMaster.CurFlag = "value"

> >
> > > ....but this just gets me an "Reference to a non-shared member requires
> > > an object reference" error on the first line. Forgive my ignorance,
> > > but is my syntax correct, or do I need to add something additional to
> > > the master page so that my module "sees" this master page?

> >
> > > I would appreciate any suggestions.
> > > Thank you.- Hide quoted text -

> >
> > - Show quoted text -

>
> Thanks for all of the replies. My situation is fairly simple: I have
> one master page (called "ProjectMgmt.Master,
> MasterPage") that has about 10 content pages.
>
> I have a function in a VB module that does some math/validation for
> me. This function is called by a button on a content page. When I
> arrive at the result, I want to change a label on the master page (so
> it is visible on all pages of the site).
>
> Is there a better way to do what I am trying to accomplish? If so, I
> would appreciate any code syntax examples you could give me. I don't
> know the "proper" names for some objects, so I am unable to follow the
> first reply's suggestion (just a newbie, I guess).
>
> Thank you again.
>
> If there is a better way to do this than
>
>

 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      25th May 2007
On May 25, 1:29 am, Kirk <lok...@hotmail.com> wrote:
> would appreciate any code syntax examples you could give me. I don't
> know the "proper" names for some objects, so I am unable to follow the
> first reply's suggestion (just a newbie, I guess).
>


Open the code-behind *.Master.vb

The name of your class you can find at the top of the file

Public Partial Class {className}

To make a proper "reference to a non-shared member" in you Module do
the following

Dim myMaster As MasterPage = New {className}
myMaster.CurFlag = "value"
.....

Should fix your problem, I hope :-)

 
Reply With Quote
 
Kirk
Guest
Posts: n/a
 
      25th May 2007
I think you are on the right track, but I still have a problem.

The name of the class is shown like this in my master page
(ProjectMgmt.Master.vb):

Partial Public Class ProjectMgmt

And the property I added is called out like this (within the above
file):

Public Property CurFlag() As String

Therefore, I followed your example (thank you!) and came up with this:

Dim myMaster As MasterPage = New ProjectMgmt
myMaster.CurFlag = "value"

The first line has no errors, but the second once has this error:

"'CurFlag' is not not a member of 'System.Web.UI.MasterPage'."

I really don't know why I am seeing this error, as once the variable
myMaster is defined, it should be able to find the public property
CurFlag (right?).

Thank you again for following up with my problem. I would appreciate
any further suggestions you might have.

 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      25th May 2007
On May 25, 2:16 pm, Kirk <lok...@hotmail.com> wrote:
> "'CurFlag' is not not a member of 'System.Web.UI.MasterPage'."


Ah, sorry! it says about 'System.Web.UI.MasterPage' because it should
be

Dim myMaster As ProjectMgmt = New ProjectMgmt

or simply

Dim myMaster As New ProjectMgmt

hope it works now

 
Reply With Quote
 
Kirk
Guest
Posts: n/a
 
      25th May 2007
Alexey, I think we are making progress, but I have yet another bug.

The code compiles correctly but I get an error as the property
attempts to assign the value. This is the property in the master
page:

Public Property CurFlag() As String
Get
Return lblFlag.Text
End Get
Set(ByVal value As String)
lblFlag.Text = value '<==Error here
End Set
End Property

When it gets to the marked line above, I get this error:

"Object reference not set to an instance of an object."

Actually, I can see this as a warning if I check this line

myMaster.CurFlag = "value"

If I hover my mouse over myMaster.CurFlag it gives this same error.
Do you have any suggestions?

Thank you again for sticking with me on this one.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting the value of a Master page's control's property using a property of the Master page Nathan Sokalski Microsoft ASP .NET 25 4th Mar 2010 03:42 AM
Making a public property on a master page visible to a content page Ex glider pilot Microsoft ASP .NET 1 25th May 2008 07:12 PM
How can I access a Master Page Public Property from a Base Page =?Utf-8?B?SmF5IFBvbmR5?= Microsoft ASP .NET 6 14th Nov 2007 12:06 PM
How To: Set Label Text Property In Master Page (From Subscribing Page)? Joey Microsoft ASP .NET 2 23rd May 2007 05:46 PM
Access property and control of master page from a customized parent page class RedHair Microsoft ASP .NET 5 23rd Mar 2006 01:39 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:01 PM.