Accessing a Master Page Property from a Module

K

Kirk

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.
 
G

Guest

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
 
G

Guest

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
 
G

Guest

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.
 
K

Kirk

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.









- 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
 
G

Guest

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")
 
G

Guest

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 :)
 
K

Kirk

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.
 
G

Guest

"'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
 
K

Kirk

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.
 
G

Guest

Alexey, I think we are making progress, but I have yet another bug.

That's great! it's not over yet :)

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?

Okay, let me think here. You have a Master page, a module referenced
to the master, and a content page, right? Where do you call
myMaster.CurFlag = "value"? In the content page?

If true, I would suggest to make your life easier, as following:

module:

Public Class Class1

Public Shared Sub ChangeCurFlag(ByVal myMaster As ProjectMgmt)

myMaster.CurFlag = "value"

End Sub

aspx content page:

Class1.ChangeCurFlag(Me.Master)
 
K

Kirk

That's great! it's not over yet :)












Okay, let me think here. You have a Master page, a module referenced
to the master, and a content page, right? Where do you call
myMaster.CurFlag = "value"? In the content page?

If true, I would suggest to make your life easier, as following:

module:

Public Class Class1

Public Shared Sub ChangeCurFlag(ByVal myMaster As ProjectMgmt)

myMaster.CurFlag = "value"

End Sub

aspx content page:

Class1.ChangeCurFlag(Me.Master)- Hide quoted text -

- Show quoted text -

Alexey - Again, I greatly appreciate you persistence in trying to
resolve my problem.

First off - the example code you gave me works! ;-)

Unfortunately, in my effort to simplify my situation for a newsgroup
post, I have run into another issue. For my testing, I created a
button on a content page that called this function. In the final
solution, I want to call this function from a custom membership
provider module function. As membership functions are called
differently than a button, our example is not working (they reside in
the App_Code folder and are called automatically from any page). This
was my mistake in explaining - sorry! I assumed that a function or
class could be created in a separate module that could then be called
from anywhere (including my membership provider class).

Anyway, look at this line we put in the content page:

Class1.ChangeCurFlag(Me.Master)

The reason this works (as I understand it) is because of this section
in the content page's designer.vb file:

Public Shadows ReadOnly Property Master() As
Project_Management.ProjectMgmt
Get
Return CType(MyBase.Master,
Project_Management.ProjectMgmt)
End Get
End Property

Obviously, I don't have this in my membership provider module/class.
I tried adding it, but I can't "wire it up" so that the module knows
which master page to use.

Is there anyway to modify what you have come up with to make the
function "stupid"? That is, to make it so that the function always
uses my one-and-only Master page reference irregardless of where it
was called from? I guess this would involve removing the "ByVal
myMaster As ProjectMgmt" part of it, but I don't know what to put in
it's place.

Again, thank you for your continued support & sorry about my
misleading comments.
 
G

Guest

Alexey - Again, I greatly appreciate you persistence in trying to
resolve my problem.

First off - the example code you gave me works! ;-)

Unfortunately, in my effort to simplify my situation for a newsgroup
post, I have run into another issue. For my testing, I created a
button on a content page that called this function. In the final
solution, I want to call this function from a custom membership
provider module function. As membership functions are called
differently than a button, our example is not working (they reside in
the App_Code folder and are called automatically from any page). This
was my mistake in explaining - sorry! I assumed that a function or
class could be created in a separate module that could then be called
from anywhere (including my membership provider class).

Anyway, look at this line we put in the content page:

Class1.ChangeCurFlag(Me.Master)

The reason this works (as I understand it) is because of this section
in the content page's designer.vb file:

Public Shadows ReadOnly Property Master() As
Project_Management.ProjectMgmt
Get
Return CType(MyBase.Master,
Project_Management.ProjectMgmt)
End Get
End Property

Obviously, I don't have this in my membership provider module/class.
I tried adding it, but I can't "wire it up" so that the module knows
which master page to use.

Is there anyway to modify what you have come up with to make the
function "stupid"? That is, to make it so that the function always
uses my one-and-only Master page reference irregardless of where it
was called from? I guess this would involve removing the "ByVal
myMaster As ProjectMgmt" part of it, but I don't know what to put in
it's place.

Again, thank you for your continued support & sorry about my
misleading comments.- Hide quoted text -

If I were you I would create a public function in the membership
provider module to return a value and call that function in the master
page.

What do you think?
 
K

Kirk

If I were you I would create a public function in the membership
provider module to return a value and call that function in the master
page.

What do you think?- Hide quoted text -

- Show quoted text -

Sounds like a good concept. I will experiment with what you have
given me and post back if I get stuck.
Thanx again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top