Connecting Tech Pros Worldwide Help | Site Map

Master page property is found but no longer set

Neil Chambers
Guest
 
Posts: n/a
#1: Jul 7 '08
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

Mark Rae [MVP]
Guest
 
Posts: n/a
#2: Jul 7 '08

re: Master page property is found but no longer set


"Neil Chambers" <nospam@nospam.comwrote in message
news:uzekg2C4IHA.4488@TK2MSFTNGP03.phx.gbl...
Quote:
>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


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Mark Rae [MVP]
Guest
 
Posts: n/a
#3: Jul 7 '08

re: Master page property is found but no longer set


"Neil Chambers" <nospam@nospam.comwrote in message
news:uzekg2C4IHA.4488@TK2MSFTNGP03.phx.gbl...
Quote:
What am I doing wrong?
Populating the property too late in the Page load cycle - the Page_Load
event of a content page fires before the Page_Load event of its MasterPage:
http://forums.asp.net/p/1247678/2297924.aspx

Try moving the MasterPage code from Page_Load to Page_Init...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Mark Fitzpatrick
Guest
 
Posts: n/a
#4: Jul 7 '08

re: Master page property is found but no longer set


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

"Neil Chambers" <nospam@nospam.comwrote in message
news:uzekg2C4IHA.4488@TK2MSFTNGP03.phx.gbl...
Quote:
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
Neil Chambers
Guest
 
Posts: n/a
#5: Jul 7 '08

re: Master page property is found but no longer set


Thank you Mark & Mark!

: n)

"Neil Chambers" <nospam@nospam.comwrote in message
news:uzekg2C4IHA.4488@TK2MSFTNGP03.phx.gbl...
Quote:
>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
Closed Thread


Similar ASP.NET bytes