LearnFast.Ninja

Access to master page property in ASP.NET

When you create public variable in your Master Page and you want to access it from is child page, and also you want the auto complete to work. In this tutorial you will see the trick how to access master page variables. This is a bit tricky, you may think that you can write this.Master etc.. but then you won't see your property. Also if you write MainMaster it won't work. So what you need to do is to cast to your specific master page name, Like this: ((MainMaster)this.Master).

Step 1: Create public property in your Master Page.

public partial class MainMaster : System.Web.UI.MasterPage

{

    public string Meta_Description { getset; }


Step 2: The trick is to cast the Master Page with the Master Page name, then it will auto complete the property. 
((MainMaster)this.Master).Meta_Description = "<meta name=\"description\" content=\"Page description.\" />";

Result:


Tags

asp.net

master-page property