Accessing Master page controls from a content page

Today I want to share with you how to use master page control from content page.
Suppose there is a Label on the Master page that we want to write to from the Content page.

1. We must include a MasterType directive at the top of the Content page ASPX file:

<%@ MasterType virtualpath="~/DetailsMaster.master" %>


2. We must include a public function in the Master page that returns a typed reference to the Label:

VB.net

Public Function LabelReference() As Label
' Create a reference to an actual label on the page
LabelReference = lblDetailsItemName
End Function
C#
public Label LabelReference()
{
// Create a reference to an actual label on the page
return lblDetailsItemName;
}

3. We can access the Label from the Content page using the Master syntax:

Master.LabelReference.Text = "Demo text";

Hope that it will help for developer.

Comments

Popular posts from this blog

The model backing the 'MyDBContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.

"This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet."

Check/Uncheck all items in a CheckBoxList using Javascript