Master page property is found but no longer set

Discussion in 'ASP .Net' started by Neil Chambers, Jul 7, 2008.

  1. 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, Jul 7, 2008
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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 Fitzpatrick, Jul 7, 2008
    #2
    1. Advertising

  3. Thank you Mark & Mark!

    : n)

    "Neil Chambers" <> wrote in message
    news:...
    >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, Jul 7, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug McCann
    Replies:
    1
    Views:
    1,030
    William Brogden
    Aug 5, 2004
  2. Zeba
    Replies:
    1
    Views:
    496
    Mark Rae
    Feb 22, 2007
  3. Joey
    Replies:
    2
    Views:
    1,034
  4. Nathan Sokalski
    Replies:
    25
    Views:
    1,437
    Nathan Sokalski
    Mar 4, 2010
  5. SerpentKiss2010
    Replies:
    0
    Views:
    2,494
    SerpentKiss2010
    Apr 6, 2011
Loading...

Share This Page