Set the property of the main page from the content page and get this property value from another content page in asp.net

I have a main page with one type of property name event. Now I want to set this property on the content page, and then the property value should be available on another content page.

Is this possible with asp.net? If so, please help me.

And yes, my content page already inherits another page that is not leading.

+5
source share
7 answers

If this property is a custom property that you added to the main page, you will need to add an ad MasterTypeto the page that includes it.

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

- , .

Page.Master.MyCustomerProperty = someValue;

: , , , ( ) , , . , "", , .

: "" . .

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());
    }
}

, , :

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;
}

CodeProject. # MSDN.

+1

, master . , , , , , , ​​ . Session - , , .

+2

:

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

:

((MyMaster)Page.Master).lblBannerText = "banner text";
+2

Master property

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

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

:

-:

public partial class BasePage : System.Web.UI.MasterPage
{ 
   private string[] _RequiredRoles = null;

   public string[] RequiredRoles
   {
     get { return _RequiredRoles; }
     set { _RequiredRoles = value; }
   }
 }

:

 public partial class _Default : System.Web.UI.Page
{
  protected void Page_Load()
  {
    Master.RequiredRoles = new string[] { //set appropriate roles };
   }
 }
0

MasterPage, , this.in .

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

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

. :

//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

All Articles