ASP.NET Interview Questions on Master Pages


What are Master Pages in ASP.NET? or What is a Master Page?
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.

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

Can Master Pages be nested?
Yes, Master Pages be nested.

What is the file extension for a Master Page?
.master

How do you identify a Master Page?
The master page is identified by a special @ Master directive that replaces the @ Page directive that is used for ordinary .aspx pages.

Can a Master Page have more than one ContentPlaceHolder?
Yes, a Master Page can have more than one ContentPlaceHolder

What is a ContentPlaceHolder?
ContentPlaceHolder is a region where replaceable content will appear.

How do you bind a Content Page to a Master Page?
MasterPageFile attribute of a content page's @ Page directive is used to bind a Content Page to a Master Page.

Can the content page contain any other markup outside of the Content control?
No.

What are the advantages of using Master Pages?
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.

What are the 3 levels at which content pages can be attached to Master Page?
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.

What is @MasterType directive used for?
@MasterType directive is used to create a strongly typed reference to the master page.

Are controls on the master page accessible to content page code?
Yes, controls on the master page are accessible to content page code.

At what stage of page processing master page and content page are merged?
During the initialization stage of page processing, master page and content page are merged.

Can you dynaimically assign a Master Page?
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";
}

Can you access non public properties and non public methods of a master page inside a content page?
No, the properties and methods of a master page must be public in order to access them on the content page.

From the content page code how can you reference a control on the master page?
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";
}
}

Can you access controls on the Master Page without using FindControl() method?
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";
}

20 comments:

  1. Good collection of question.
    But, answer of last question is incomplete.
    "Can you access controls on the Master Page without using FindControl() method?"

    Control of master page can not get in this way.
    Need to follow following steps to get controls without using FindControl() method -

    1. create public property in master page that returns textbox

    public TextBox MTB
    {
    get { return TextBox1; }

    }
    2. Add MasterType directive in content page


    3. Use Master property to of Page to get above property.
    String TBValue = This.Master.MTB.Text

    ReplyDelete
  2. Keyur Patel from cygnet infotech pvt ltdAugust 24, 2009 at 11:12 AM

    I think it's good questions for master page related

    ReplyDelete
  3. good collection of question but by using javascript also we can acess the master page with out using findcontrol

    ReplyDelete
  4. Its damn informative:)Thanks a lot it helps me

    ReplyDelete
  5. What are the 3 levels at which content pages can be attached to Master Page?

    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.


    any one can explain breifly with code, how u are doing the At the application level and At the folder level ???
    please try to help me out in this itself or reach me out at manju.shine.007@gmail.com

    ReplyDelete
  6. Its Good Questions related with Master pages, Good for interview also.......Thanx

    ReplyDelete
  7. While we request a page which one is loaded first whether master or content page?

    ReplyDelete
    Replies
    1. Master page is merged into the content page during the initialization stage of page processing. Once it is merged Master Page is Treated as a control in the content pages. So as a general Rule the Load event are occured from outermost control to innermost control. So Content page load event are executed first.

      Delete
  8. can we handle event in default web page which event occur in master page

    ReplyDelete
  9. When requesting a page, content page is loaded first

    ReplyDelete
  10. Master Page file at Applicaiton Lavel:
    By including a tag in web.config under system.web tag like this:

    ReplyDelete
  11. Thank You sir for giving valuable information on master pages

    ReplyDelete
  12. What is the diff b/w User Control & Mater Pages?

    ReplyDelete
  13. Can we apply another master's content to the content page of this master page?

    ReplyDelete
  14. till what level we can nest master page...?? Is there any limitation or we can nest master page to any level??...... this question was asked in TCS interview...
    Can Someone tell me the answer..

    ReplyDelete
  15. Hi Venkat
    Please reply for one interview question:
    If you have a user control on your Master Page then what will bet he sequence of execution. (Like 1. Content Page 2. User Control 3. Master Page). Please clarify. Thanks

    ReplyDelete
  16. In application one content page and one master page . which is first load..
    using page load cycle.

    ReplyDelete
  17. Sequence of execution(Content page,User Control, Master Page)

    ReplyDelete
  18. sorry my pervious answer is wrong..

    Sandeep Nath from IBM India.

    When will page load event called firest called
    1)Content page.
    2)Master Page.
    3)User Control

    When will page_Init event called firest called
    1)User Control.
    2)Master Page.
    3)Content page.
    Please Follow above answer guys.

    ReplyDelete

If you are aware of any other asp.net questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us