Reference Controls in MasterPage?

C

clintonG

I'm trying to build a class file in App_Code to reference LinkButton
controls located in a MasterPage.
I know how to reference the controls in the MasterPage from Content pages
but I'm having a problem when moving all the code from the various Content
pages into a class. The erroer

// basic use case (base class inherits System.Web.UI.Page)
public class HeaderTabbedNavigation : GlobalBaseClass
{
static void IsEnabled(bool enabled)
{
if(enabled == true)
MemberLoginStatus.Enabled = true;
return;
}
}

// direct access (fails)
HeaderTabbedNavigation.IsEnabled(true);

// error
'MemberLoginStatus' does not exist in the current context

Is this enough information for somebody to tell me what am I doing wrong and
how should I start thinking when creating classes in this context?

<%= Clinton Gallagher
 
C

CaffieneRush

Just a guess. You're trying to access a non static member from a static
method.
 
C

clintonG

I understand what you mean. As it turns out, it seems a class may have but
one static constructor which takes no parameters but I still get the same
error when not using the static keyword and I have to learn how to access
controls in the MasterPage this way or I'll never master class design.


<%= Clinton Gallagher
 
C

CaffieneRush

Have you tried to use FindControl to get a reference to
MemberLoginStatus within the Master Page.

Eg. within the content page.
Dim MemberLoginStatus As LinkButton =
Me.Master.FindControl("MemberLoginStatus")
MemberLoginStatus.Enabled = True

Or you can expose MemberLoginStatus within the master as a property.
Eg. within the master page with a classname of CorporateMaster.
Public ReadOnly Property MemberLoginStatus As LinkButton
Get
Return MasterMemberLoginStatus
End Get
End Property

'Within the content page.
Ctype(Me.Master, CorporateMaster).MemberLoginStatus.Enabled = True
 
C

clintonG

Yes, I've done both already used elsewhere in the application. Intellisense
will not "see" the FindControl method in the class.

<%= Clinton Gallagher
 
C

CaffieneRush

That's odd.
I tested both techniques and verified that they both work before
posting my suggestion.
And there was intellisence support for FindControl() as well. Of
course, the page was not inheriting from a base class page.

Just to be sure, if your master page was set in the base class but not
the sub class then I would do something like the following:
'vb.net code
Dim MyLinkB As LinkButton =
Me.MyBase.Master.FindControl("MyLinkButton")

Finally, check if you've referenced your master page properly.
 
C

clintonG

I sure appreciate your insights but note I've used properties (early bound)
and the FindControl method (late bound) myself but I'm not trying to inherit
from the Master from a content page. I've been having this conversation in
the Forums [1] and I'm going to try the suggestion to modify the Master to
inherit from a base class itself and then implement properties on the
controls I need to access.

<%= Clinton Gallagher

[1] http://forums.asp.net/1233555/ShowPost.aspx
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top