How to Set MasterPage Properties in PostBackEvent?

Last post 10-31-2007 8:48 AM by JBelthoff. 6 replies.

Sort Posts:

  • How to Set MasterPage Properties in PostBackEvent?

    10-25-2007, 10:53 AM
    • Member
      541 point Member
    • JBelthoff
    • Member since 08-17-2002, 5:37 PM
    • New Jersey, USA
    • Posts 134

    Hi All,

    I have a MetaRefresh Property in My masterpage.cs. On a normal postback it gets written fine, however in a PostBackEvent it does not get written at all.

    How does one write to a MasterPageProperty from a PostBackEvent.

    For instance...

    This Works, 

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                ((ASP.masterpage_master)Page.Master).MetaRefresh = string.Empty;
    
            }
            else
            {
                ((ASP.masterpage_master)Page.Master).MetaRefresh = "<meta http-equiv=\"refresh\" content=\"6;url=default.aspx\">";
            }
        }
     

    But this does not! 

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (1 == 1)
            {
                ((ASP.masterpage_master)Page.Master).MetaRefresh = "<meta http-equiv=\"refresh\" content=\"6;url=default.aspx\">";
            }
        }
     

     

    John Belthoff
    Dodge, Duck, Dip, Dive & Dodge
    If a man can dodge a wrench, he can dodge a ball!
  • Re: How to Set MasterPage Properties in PostBackEvent?

    10-28-2007, 11:37 PM

    Hi,

    Base on your description, you set a property in the master page and want to acess this property in the content page, right?

    If you want to access the property that declared in the master page codebehind ,  All your need is an @ MasterType directive set to the master page. let the ASP.NET page parser generate a strongly typed Master property by adding an @ MasterType directive to your ASPX file, as shown below.

    <%@ MasterType VirtualPath="~/otc.master"  %>

     

    The MasterType directive will instruct the runtime to add a new Master property to code-generated file for the page. The new property will return a reference matching the MasterType. With a MasterType directive in place for our web form, we don't need a cast. 

    For more information, you can read this article:  http://www.odetocode.com/Articles/419.aspx

    Hope it helps.

    Amanda Wang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: How to Set MasterPage Properties in PostBackEvent?

    10-29-2007, 10:03 AM
    • Member
      541 point Member
    • JBelthoff
    • Member since 08-17-2002, 5:37 PM
    • New Jersey, USA
    • Posts 134

    Actually no that is not the problem at all. I have no problem writing a property in the page_load or postback, but if I do it in the PostBackEvent it does not make the final render of the page. This is apparently because the MasterPage sets the properties before the PostBackEvent fires.

    I am looking for a way to alter that MasterPage Property before the page renders from a button_click or other PostBackEvent.

    Does that Make sense?

    John Belthoff
    Dodge, Duck, Dip, Dive & Dodge
    If a man can dodge a wrench, he can dodge a ball!
  • Re: How to Set MasterPage Properties in PostBackEvent?

    10-30-2007, 3:13 AM

    Hi,

    I get your mean, you want to change the MetaRefresh's  value in content page's button click event, but  failed, right?

    I created a test application, but did not repro your problem, have you debuged your code and make sure the button_click event be executed when you click on the button control? Or you can create a new simple application and have a try to check the problem still exists.

    Below is my test code:

    1. the maste page codebehind:

     public string UserName
        {
            get
            {
                return this.txtLoginUser.Text;
            }
            set
            {
                this.txtLoginUser.Text = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

    2. The content page's aspx file:

    <%@ Page Language="C#" MasterPageFile="~/TestMasterPage/masterpage/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="TestMasterPage_masterpage_Default" Title="Untitled Page" %>
    <%@ MasterType VirtualPath="~/TestMasterPage/masterpage/MasterPage.master" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="middleContent" Runat="Server">
        <asp:Button ID="Button1" runat="server"  Text="Button" OnClick="Button1_Click" />
    </asp:Content>

    3. The content page's codebehind:

    protected void Page_Load(object sender, EventArgs e)
        {          

    }
        protected void Button1_Click(object sender, EventArgs e)
        {
                   this.Master.UserName = "Tom";
        }

    If these cannot help you, if you can , could you provided the more related Code, we may be able to help further.

    Hope it helps.
    Amanda Wang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: How to Set MasterPage Properties in PostBackEvent?

    10-30-2007, 8:35 AM
    • Member
      541 point Member
    • JBelthoff
    • Member since 08-17-2002, 5:37 PM
    • New Jersey, USA
    • Posts 134

    Yes here it my code:

    Master Page 

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class MasterPage : System.Web.UI.MasterPage
    {
        private String _strstring;
        /// <summary>
        /// Property strString (String)
        /// </summary>
        public String strString
        {
            get { return this._strstring; }
            set { this._strstring = value; }
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(_strstring);
        }
    }
     Default.aspx 
    <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
    <%@ MasterType VirtualPath="~/MasterPage.master" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </asp:Content>
     Default.aspx.cs 
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            ((ASP.masterpage_master)Page.Master).strString = "Hello World!";
        }
    }
     The above code does not write to the page. However the below code (default.aspx.cs) does.  
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(this.IsPostBack)
                ((ASP.masterpage_master)Page.Master).strString = "Hello World!";
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            
        }
    }
     
    John Belthoff
    Dodge, Duck, Dip, Dive & Dodge
    If a man can dodge a wrench, he can dodge a ball!
  • Re: How to Set MasterPage Properties in PostBackEvent?

    10-30-2007, 11:05 PM

    Hi,

    Thanks for you code, I think the problem is the event order of the master page and the content page.

    When you click on the button, the order of the event is as below:

     Firstly, The content page's load event.

     Secondly, The master page's load event.

    Finally, The content page's button click event.

    From the code, we can see you write the response the result to the page in the master page's page load event.

    Therefore, this is the reason why you cannot see the result when you write the code ((ASP.masterpage_master)Page.Master).strString = "Hello World!"; in the content page's button click.

    Hope it helps.

     

    Amanda Wang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: How to Set MasterPage Properties in PostBackEvent?

    10-31-2007, 8:48 AM
    • Member
      541 point Member
    • JBelthoff
    • Member since 08-17-2002, 5:37 PM
    • New Jersey, USA
    • Posts 134

    Yes thanks. However if you read my first and second posts you will notice that I am already aware of what you wrote above.

    I am looking to find out how to make this happen. I already know why it is not.

    Any ideas? Anyone?

    John Belthoff
    Dodge, Duck, Dip, Dive & Dodge
    If a man can dodge a wrench, he can dodge a ball!
Page 1 of 1 (7 items)