Interview Questions & Answer on Master Pages

Q : What are Master Pages in ASP.NET? or What is a Master Page?

Ans : ASP.NET master pages allow you to create a consistent layout for the pages in your application. A single master page defines the look and feel and standard behavior that you want for all of the pages (or a group of pages) in your application. You can then create individual content pages that contain the content you want to display. When users request the content pages, they merge with the master page to produce output that combines the layout of the master page with the content from the content page.

Q : What are the 2 important parts of a master page?

Ans : The following are the 2 important parts of a master page
1. The Master Page itself
2. One or more Content Pages

Q : Can Master Pages be nested?

Ans : Yes, Master Pages be nested.

Q : What is the file extension for a Master Page?

Ans : File extension for a Master Page is .master

Q : How do you identify a Master Page?

Ans : The master page is identified by a special @ Master directive that replaces the @ Page directive that is used for ordinary .aspx pages.

Q : Can a Master Page have more than one ContentPlaceHolder?

Ans : Yes, a Master Page can have more than one ContentPlaceHolder.

Q : What is a ContentPlaceHolder?

Ans : ContentPlaceHolder is a region where replaceable content will appear.

Q : How do you bind a Content Page to a Master Page?

Ans : MasterPageFile attribute of a content page's @ Page directive is used to bind a Content Page to a Master Page.

Q : Can the content page contain any other markup outside of the Content control?

Ans : No.

Q : What are the advantages of using Master Pages?

Ans : 1. They allow you to centralize the common functionality of your pages so that you can make updates in just one place.
2. They make it easy to create one set of controls and code and apply the results to a set of pages. For example, you can use controls on the master page to create a menu that applies to all pages.
3. They give you fine-grained control over the layout of the final page by allowing you to control how the placeholder controls are rendered.
4. They provide an object model that allows you to customize the master page from individual content pages.

Q : What are the 3 levels at which content pages can be attached to Master Page?

Ans : At the page level - You can use a page directive in each content page to bind it to a master page.
At the application level - By making a setting in the pages element of the application's configuration file (Web.config), you can specify that all ASP.NET pages (.aspx files) in the application automatically bind to a master page.
At the folder level - This strategy is like binding at the application level, except that you make the setting in a Web.config file in one folder only. The master-page bindings then apply to the ASP.NET pages in that folder.

Q : What is @MasterType directive used for?

Ans : @MasterType directive is used to create a strongly typed reference to the master page.

Q : Are controls on the master page accessible to content page code?

Ans : Yes, controls on the master page are accessible to content page code.

Q : At what stage of page processing master page and content page are merged?

Ans : During the initialization stage of page processing, master page and content page are merged.

Q : Can you dynaimically assign a Master Page?

Ans : Yes, you can assign a master page dynamically during the PreInit stage using the Page class MasterPageFile property as shown in the code sample below.
void Page_PreInit(Object sender, EventArgs e)
this.MasterPageFile = "~/MasterPage.master";

Q : Can you access non public properties and non public methods of a master page inside a content page?

Ans : No, the properties and methods of a master page must be public in order to access them on the content page.

Q : From the content page code how can you reference a control on the master page?

Ans : Use the FindControl() method as shown in the code sample below.
void Page_Load()
// Gets a reference to a TextBox control inside
// a ContentPlaceHolder
ContentPlaceHolder ContPlaceHldr = (ContentPlaceHolder)Master.FindControl ("ContentPlaceHolder1");
if(ContPlaceHldr != null)
TextBox TxtBox = (TextBox)ContPlaceHldr.FindControl("TextBox1");
if(TxtBox != null)
TxtBox.Text = "TextBox Present!";
// Gets a reference to a Label control that not in
// a ContentPlaceHolder
Label Lbl = (Label)Master.FindControl("Label1");
if(Lbl != null)
Lbl.Text = "Lable Present";

Q : Can you access controls on the Master Page without using FindControl() method?

Ans : Yes, by casting the Master to your MasterPage as shown in the below code sample.
protected void Page_Load(object sender, EventArgs e)
MyMasterPage MMP = this.Master;
MMP.MyTextBox.Text = "Text Box Found";

  « Previous    | Next »
Leave a Reply
Name (required)
Email (will not be published) (required)
Web Site

List of Top Hospitals in India