Master Pages - Strongly-Typed control access

G

Guest

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:
<%@ master masterpagefile="~/Root.master" language="VB"
codefile="Home.master.vb" inherits="Home_Master" %>
<%@ mastertype virtualpath="~/Root.master" %>

I can access *properties* on the Master Page in a strongly typed way, but..

Is there a way of accessing *controls* in strongly-typed way (any solution
including some crazy reflection code is permissible) without using
FindControl?

Thanks!
 
S

Steven Cheng[MSFT]

Hi Boris,

I've seen some other threads you posetd in the newsgroup and have posetd
some suggestion there.

As for the question about referencing control in Master page through
Strong-typed means, I've ever found many thread discussing on this in the
newsgroup or other community.

IMO, to locate a certain control on the page, if it is not directly
referenced through a page variable, the reasonable means is to use
"FindControl" method to locate it(from Page or some other nested
namingcontainer). This is because Control collection is the only reliable
way to navigate to any sub controls in the page's control Tree. Generally,
I will use the ASP.NET page's output trace to inspect control tree so as to
get how I should write code to find that control. You can turn on the
output trace for page in the @Page directive like:

<%@ Page Trace="true" ......... %>

You can find the control tree of the whole page in the output trace:

#Reading ASP.NET Trace Information
http://msdn2.microsoft.com/en-us/library/kthye016.aspx


For your scenario here, to access the control(in master page) from content
page by strong-typed means, what I would do is defining some public
properties in the master page which will do the control locating task and
return the certain control's instance. e.g.

suppose we have the following template in master page(control.master):
========control.master=============
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0" style="width:
100%; height: 100%">
<tr>
<td style="height: 80px;width:100px">
<asp:Image ID="imgLogo" runat="server"
ImageUrl="http://www.asp.net/i/www_asp_net_logo.gif" />
</td>
<td>
<asp:Label ID="lblSubject" runat="server" Text="Default
Subject" Font-Size="30"></asp:Label>
</td>
</tr>
<tr>
<td valign="top" colspan="2">
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</td>
</tr>
</table>
</div>
</form>
====================

and in the master's codebehind, we define two public properties to expose
the two controls in it

=========master code behind===============

public partial class Master_control : System.Web.UI.MasterPage
{
public Label SubjectLabel
{
get
{
return lblSubject;
}
}

public Image LogoImage
{
get
{
return imgLogo;
}
}

........................
}


==============================


Thus, in the content page which applied this master page(and reference it
through @MasterType directive), we can use the following code to access the
controls in master page as strong-typed objects:

=================================

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Master_control master = Page.Master as Master_control;

master.SubjectLabel.Text = "Customized Sujbect";
master.LogoImage.ImageUrl =
"http://www.msnbc.msn.com/images/msnbc/logo01.gif";
}
}

======================================


In addition, if you do not want to always @MasterType directive(which will
affect page's dynamic compilation sequence), you can consider define a
separate interface, and let your master page class implement this
interface, e.g:


public interface IMasterHelper
{
public control GetControlByID(string id);
}


public partial class Master_control : System.Web.UI.MasterPage,
IMasterHelper
{
....
public Control GetcontrolByID(string id)
{
return Page.FindControl(id);
}
}


Thus, in content page, you can convert Page.Master to that interface type
and then query control through the interface methods.

Just some of my consideration, hope this helps you some.


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steven Cheng[MSFT]

Hello Boris,

Have you got any further progress or ideas on this issue or does my
suggsetion helps a little? If there is anything else we can help, please
feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi Steven,

I was really looking for a way to directly access the controls, i.e.:
Master.Page.MyControl.CssClass = "hello"

But without creating a property, which is doubling everything up.

Someone did point out to me that the only reason I can't access the controls
is that I'm being rather thick, and that they are of course declared
"Protected". I'd forgotten about that since moving to ASP.NET2.0 where the
partial classes hide the declarations of all the controls.

So, my question is now a bit more esoteric.

Is there a way to either:
a) Turn off the partial classes for a single page
or
b) Over-ride some of the controls - to get them declared as "Public" rather
than "Protected"

I hope that question makes some sense :)

- Boris
 
S

Steven Cheng[MSFT]

Thanks for the followup Boris,

Now I've got what you want is directly expose the control member in the
MasterPage as public. Due to the ASP.NET 2.0 page's new compilation model,
we can not directly specify the control member properties in page's
codebeind.

So far what I can get is using a Base class for our page(which contains the
public control member variable) and also use the "CodeFileBaseClass"
attribute in @Page/@master directive to associate this base page class. In
such case, the ASP.NET runtime will check whether there is any control's ID
identical to any page member variable in the base class. If exists, the
control is linked to the variable in base class at dynamic compilation
time.

#@ Page
http://msdn2.microsoft.com/en-us/library/ydy4x04a.aspx

For example, suppose we define a base master page class as below(define a
public variable to expose a Label control on the master page):

#put in App_code folder
===================
public class BaseMasterPage : MasterPage
{
public Label lblMessage;

........................
}
==============================


Then, in our actual master page, we use it as base class:

==============
public partial class Master_control : BaseMasterPage
{
..............
}
===============

Also, in the .master file, we need to associate the base page in the
@master directive as below:

==========================

<%@ Master .............. CodeFile="control.master.cs"
Inherits="Master_control" CodeFileBaseClass="BaseMasterPage" %>

=======================

After that, we can directly use this field (in base master class) to
reference the control in content page.

============================
protected void Page_Load(object sender, EventArgs e)
{
Master_control master = Page.Master as Master_control;

master.lblMessage.Text = "Modified Message: " +
DateTime.Now.ToLongTimeString();

}
============================


Anyway, I still think define a public property(which encapsulate the code
to find control and return it ) will be better since it can help us add
some customized code or error handling code. Also, for those control which
is not directly refereneable by page variable (may be contained in other
control as child control), the above approach won't work.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Joined
Nov 10, 2006
Messages
1
Reaction score
0
Hi Stevan,
I have an issue something similar to the above. My constraint is, I can't offer to declare the Properties again in the masterpage, since I use master page just for content place holders. All the controls are dealt in content aspx page and its code behind. All I need is dynamically change the Properties of Controls from content page's code behind.
I've tried all the possible ways but no luck.

Do you’ve any idea what is going wrong here?? (Pls below code)
Thanks in advance.
-sj

Here is the code snippet

1. This is the declaration in the master page (masterPage.master)
<asp:ContentPlaceHolder ID="contentID" runat="server">
</asp:ContentPlaceHolder>


2.This is the declaration in the content page (content.aspx)
<asp:Content ID="contentID" runat="Server" ContentPlaceHolderID="contentDetails">
<asp:Label ID="Label1" runat="server" Text="Fisrt Text"></asp:Label>
</asp:Content>

3. Onpageload I'm trying to change the value of Label1 to "Second Text"
I have written the below code to do so but it is doing nothing. When the page loads I see "First Text", where in I'm expecting "Second Text"


Here are the couple of approach I took
1. Find control method

In conent.aspx.cs on Page_Load event I have the below code.

ContentPlaceHolder cnt;
cnt= (ContentPlaceHolder) Page.Master.FindControl("contentDetails");

Label lbl;
lbl= (Label) FindControlRecursive(this.Master, "Label1");
lbl.Text = "Second Text";


2. I Tried setting <%@ MasterType VirtualPath="~/masterPage.master" %>
but no luck..
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top