Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Master page variables in child page in ASP.NET

Tags:

c#

asp.net

Here is my master page file. I need to pass strName, id, url, starttime, etc. to my Child page. I know we can write this logic in our Child page, but, I would like to access this Master Page variable in my Child page only.

I cannot write this logic in each set/get method. While accessing these variable in the Child page, I am getting null values. basically here the master pageload calls after the child pageload calls over:

  1. MASTER PAGE NAME: MyMasterPage

    public partial class MyMasterPage: MasterPage { public string strName = string.Empty; public string id= string.Empty; public string url = string.Empty; public string startTime = string.Empty; public string endTime = string.Empty; public string remoteUrl = string.empty;

      public void Page_Load(object sender, EventArgs e)
      {
    
         DataTable dtEventTable = DataAccessManager.GetEventInfo(Connection);
    
         if (dtEventTable.Rows.Count > 0)
         {
                strName = dtEventTable.Rows[0]["NAME"].ToString(); 
                id = dtEventTable.Rows[0]["ID"].ToString(); 
                url= dtEventTable.Rows[0]["URL"].ToString(); 
                starttime = dtEventTable.Rows[0]["starttime"].ToString(); 
                endtime = dtEventTable.Rows[0]["endtime"].ToString(); 
                remotelive = dtEventTable.Rows[0]["remotelive"].ToString(); 
                // assume that strName = "TCG",id=5, startime=20111001 etc.
         }
     }
    

    }

like image 269
Padmanabha Avatar asked Oct 01 '11 10:10

Padmanabha


1 Answers

string name = ((MyMasterPage)this.Master).strName;

Read Working with ASP.NET Master Pages Programmatically

like image 62
Muhammad Hasan Khan Avatar answered Oct 18 '22 04:10

Muhammad Hasan Khan