Monday, April 30, 2007

ASP.Net Custom Control accessing Master Page

Problem: I have a custom control (.ascx), and want to access a label control on the masterpage.

Default.aspx uses MasterPage.Master
CtrlChild.ascx is embedded into Default.aspx


Solution:
public partial class CtrlChild : System.Web.UI.UserControl
{

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{

MasterPage mp = (MasterPage)this.Parent.Page.Master;
Label lblUN = (Label)mp.FindControl("lbl_master_user_name");

}
}
}

No comments: