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.