Master page property is found but no longer set

  • Thread starter Thread starter Neil Chambers
  • Start date Start date
N

Neil Chambers

I have a public property defined in my master page

public partial class MainMaster : System.Web.UI.MasterPage
{
string UserName;

public string userName
{
get
{
return UserName;
}
set
{
UserName = value;
}
}

protected void Page_Load(object sender, EventArgs e)
{
userName = "MyUser";
}
}

I have set up the MasterType directive on a child page and can locate the
userName property using intellisense - but when the child page loads the
property is no longer set;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = Master.userName; // this does not appear
TextBox1.Text += "\nhowdy"; //this does appear
}
}

What am I doing wrong?

Cheers,
n
 
Have you tried setting the property value in the masterpage earlier? The
problem here is, which occurs first, the Page_Load in the Master, or in the
inherited Page. It may end up surprising you. Try doing it during the OnInit
event of the Master. When you set the data, set it to the UserName local
variable and not the property as every property activity has a small bit of
overhead and it's best to avoid it whenever possible.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression
 
Back
Top