473,320 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Master Pages - Strongly-Typed control access

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!
Aug 23 '06 #1
4 3946
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.

Aug 24 '06 #2
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.

Aug 28 '06 #3
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

"Steven Cheng[MSFT]" wrote:
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.

Aug 29 '06 #4
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.



Aug 30 '06 #5

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

Similar topics

5
by: Murphy | last post by:
I have read the articles by Paul Wilson regarding master pages, and apart from being totally confused I would like to clarify the following before investing any further time on researching these...
1
by: Sasha | last post by:
Hi all asp.net proffesionals out there, How do you implement master pages in your applications? What libraries do you use and what have been your experience? Sasha
5
by: Michael Herman \(Parallelspace\) | last post by:
1. What are some compelling solutions for using Master/Content pages with Web Pages? 2. If a content area has a web part zone with web parts, what is the user experience like when "editting" the...
20
by: Alan Silver | last post by:
Hello, In classic ASP, I used to use two include files on each page, one before and one after the main content, to provide a consistent layout across a web site. That way I could just change the...
1
by: Alan Silver | last post by:
Hello, I am just experimenting with master pages, and am trying to add a content placeholder in the <head> section, so that individual pages can set their own page title and meta tags. The...
3
by: Paul | last post by:
I found a great site for CSS tempaltes, http://blog.html.it/layoutgala/ . I downloaded them, and played around with them. Then I tried to use the template in as ASP.Net MasterPage, but everything...
17
by: Rob R. Ainscough | last post by:
Again another simple concept that appears NOT to be intuitive or I'm just stupid. I've read the WROX book and the example doesn't actually show how the .master page links in the other content...
2
by: news.sbcglobal.net | last post by:
I hope I can explain this well enough to understand. I have a master page that is used by almost all of the pages in my site. On the master page is a table. In one of the cells in this table, I...
2
by: Argirop | last post by:
I have a page Default.aspx. Its first line is the following: <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="default.aspx.cs"...
3
by: Rich | last post by:
Hi, I want to use 2 master pages, one for the main part of the site, the other for the admin pages. They are quite similar, with many shared elements. Ideally I would like to have a parent...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.