Update a control on a Master Page

Creating a Master Page in Visual Studio makes simplifies the development of ASP.NET websites. Master Page's allow for the creation of consistent and standard layouts (template) for a group of pages or an entire website. ContentPlaceHolders are used to designate where on a page based on a Master Page will have variable content. Sometimes it may be necessary to update a control that is part of the Master Page, rather within the ContentPlaceHolder area. There are several ways to update a control on a Master Page. A page's Master property allows for access to a page’s Master Page content. The following code is one example of how to update a control on a master page. The code would be run from the page based on a Master Page.

Image MasterLogo = (Image) Master.FindControl ( "Logo" );
        if ( MasterLogo != null )
        {
            MasterLogo.ImageUrl = "~/images/logo.gif";
        }

        Label MasterLabel = (Label) Master.FindControl ( "Label1" );
        if ( MasterLabel != null )
        {
            MasterLabel.Text = "Set from page";
        }



   

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading