La solution la plus simple est d'utiliser une interface comprenant les contrôles en propriétés. De l'implémenter dans la MasterPage. Ensuite dans la page utilisant la MasterPage récupérer la propriété Master et faire un petit cast pour obtenir l'interface et ainsi accéder aux contrôles.
Petit exemple trivial avec un Label:
Vb
' L'interface
Public Interface IMasterPage
' Le label que l'on veut rendre accessible
ReadOnly Property MonLabel() As Label
End Interface
'--------------------------------------------------------------------
' La MasterPage
Public Partial Class MasterPage
Inherits System.Web.UI.MasterPage
Implements IMasterPage
Public ReadOnly Property MonLabel() As System.Web.UI.WebControls.Label Implements IMasterPage.MonLabel
Get
' Le label de la masterpage Label1
Return Me.Label1
End Get
End Property
End Class
'--------------------------------------------------------------------
' L'accès au label via la page qui utilise la masterpage
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim masterpage As IMasterPage = CType(Me.Master, IMasterPage)
masterpage.MonLabel.Text = "Mon text"
End Sub
End Class
C#
// L'interface
public interface IMasterPage
{
// Le label que l'on veut rendre accessible
Label MonLabel{ get; }
}
// -------------------------------------------------------------------
// La MasterPage
public partial class MasterPage :
System.Web.UI.MasterPage,
IMasterPage
{
public Label MonLabel
{
get
{
// Le label de la masterpage Label1
return this.Label1;
}
}
}
// --------------------------------------------------------------------
// L'accès au label via la page qui utilise la masterpage
partial class _Default :
System.Web.UI.Page
{
protected void Page_Load(Object sender, System.EventArgs e)
{
IMasterPage masterpage = Me.Master as IMasterPage;
masterpage.MonLabel.Text = "Mon text";
}
}
