ASP.NET how to access public properties?

前端 未结 5 704
难免孤独
难免孤独 2020-12-12 08:06

I have two pages page1.aspx and page2.aspx, both have code behind with partial classes. How do i access public property message on page1.aspx from page2.aspx ?



        
5条回答
  •  盖世英雄少女心
    2020-12-12 08:37

    If the one is the Master page, and the other is the page that use the master.

    The Master Page

    
        

    and the code behind

    public partial class Dokimes_StackOverFlow_MasterPage : System.Web.UI.MasterPage
    {
        public string TextToMaster
        {
            get { return txtOnMaster.Text; }
            set { txtOnMaster.Text = value; }        
        }
    
    
        protected void Page_Load(object sender, EventArgs e)
        {
            // here I find the control in the client page
            Control FindMe = ContentPlaceHolder1.FindControl("txtOut");
    
            // and if exist I set the text to client from the master   
            if (FindMe != null)
            {
                ((Literal)FindMe).Text = "Get from Master Page";
            }
        }
    }
    

    and now the Page1.aspx that have the previus master page

    
          
    
    

    and the code

    protected void Page_Load(object sender, EventArgs e)
    {
        // here I set the text on master page from client
        ((Dokimes_StackOverFlow_MasterPage)Master).TextToMaster = "Set from Client";
    }
    

提交回复
热议问题