set property of master page from content page and get that property value from another content page in asp.net

前端 未结 7 860
时光取名叫无心
时光取名叫无心 2021-01-03 01:55

I have a master page with one property name event type. Now I want to set this property from a content page and then that property value should be available to another conte

相关标签:
7条回答
  • 2021-01-03 01:59

    Add a property to the master page:

    public string lblBannerText { get { return lblBanner.Text; } set { lblBanner.Text = value; } }
    

    Then, get the master of the page and cast it to the master class type:

    ((MyMaster)Page.Master).lblBannerText = "banner text";
    
    0 讨论(0)
  • 2021-01-03 02:03

    If this property is a custom property that you added to your master page, then you'll have to add a MasterType declaration to the page that is incorporating it.

    <%@ MasterType 
        virtualpath="~/Path/To/Your.master" 
    %>
    

    This allows the web site or application to know the specific type of the master page at compile time and allows you to access it as you would any other property in a control.

    Page.Master.MyCustomerProperty = someValue;
    

    Edit: As a side note, in getting this property down to your next control, it would probably be best to create (and raise) a custom event indicating the property has changed. This way a number of controls can subscribe to the event and "self-update" without having to be concerned with the timing of when the property is set.

    Example: In your master page you can define an event as a "global" variable. Then in your property you can raise this event.

    public event EventHandler myPropertyChanged;
    public delegate void MyPropertyChanged(object sender, EventArgs e);
    
    //...
    
    public string MyProperty
    {
        get
        {
           return _myProperty;
        }
        set
        {
            _myProperty = value;
            if (myPropertyChanged != null)
                myPropertyChanged(this, new EventArgs());
        }
    }
    

    Then in your other controls, you can subscribe to this event to know when it changes:

    protected void Page_Load(object sender, EventArgs e) 
    {
        Page.Master.MyPropertyChanged += new EventHandler(MasterPropertyChanged);
    }
    
    protected void MasterPropertyChanged(object sender, EventArgs e) 
        //Rememeber you need the VirtualType in order for this event to be recognized
        SomeLocalValue = Page.Master.MyProperty;
    }
    

    A good step-by-step tutorial of this process can be found on CodeProject. A good c# custom events tutorial can be found on the MSDN.

    0 讨论(0)
  • 2021-01-03 02:06

    You can try this

    Create a property in your master page and you access it from content page:

    Master page:

    public partial class BasePage : System.Web.UI.MasterPage
    { 
       private string[] _RequiredRoles = null;
    
       public string[] RequiredRoles
       {
         get { return _RequiredRoles; }
         set { _RequiredRoles = value; }
       }
     }
    

    Content Page:

     public partial class _Default : System.Web.UI.Page
    {
      protected void Page_Load()
      {
        Master.RequiredRoles = new string[] { //set appropriate roles };
       }
     }
    
    0 讨论(0)
  • 2021-01-03 02:12

    the easy way to get or set MasterPage's property, just like this.in content page do the follow.

    protected override void Render(HtmlTextWriter writer)
    {
        var master = this.Master as Site;
        master.SiteName += "|网站首页";
        base.Render(writer);
    }
    

    and define property in master page

    public partial class Site : System.Web.UI.MasterPage
    {
        public string MetaDescription { get; set; }
        public string MetaKeywords { get; set; }
        public string SiteName { get; set; }
        public SiteSettings siteSettings { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            siteSettings = SettingsFactory<SiteSettings>.Get();
            MetaDescription = siteSettings.SearchMetaDescription;
            MetaKeywords = siteSettings.SearchMetaKeywords;
            SiteName = siteSettings.SiteName;
        }
    }
    
    0 讨论(0)
  • 2021-01-03 02:17

    I had a similar problem. Here's what I did:

    //Id the form tag and place a runat server
      <form id="form1" runat="server" >
    
    //In C# Masterpage.cs - Expose the forms properties
     public System.Web.UI.HtmlControls.HtmlForm Form1 {
                get { return form1; }
                set { form1 = value;
                } }
    //In C# Consuming page add this to pageLoad using UniqueId
     ((SiteMaster)Page.Master).Form1.DefaultButton = btnSearchUsersLink.UniqueID;
    
    0 讨论(0)
  • 2021-01-03 02:18

    You can use Master property of your page and access to your property

    Link : http://msdn.microsoft.com/fr-fr/library/system.web.ui.page.master.aspx

    Sample

    ContentPlaceHolder mpContentPlaceHolder;
        TextBox mpTextBox;
        mpContentPlaceHolder = 
          (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
        if(mpContentPlaceHolder != null)
        {
            mpTextBox = 
                (TextBox) mpContentPlaceHolder.FindControl("TextBox1");
            if(mpTextBox != null)
            {
                mpTextBox.Text = "TextBox found!";
            }
        }
    
    0 讨论(0)
提交回复
热议问题