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";
}
Subscribe to:
Post Comments (Atom)
Good collection of question.
ReplyDeleteBut, 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
I think it's good questions for master page related
ReplyDeletegood collection of question but by using javascript also we can acess the master page with out using findcontrol
ReplyDeleteIts damn informative:)Thanks a lot it helps me
ReplyDeleteWhat are the 3 levels at which content pages can be attached to Master Page?
ReplyDeleteAt 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
Its Good Questions related with Master pages, Good for interview also.......Thanx
ReplyDeleteWhile we request a page which one is loaded first whether master or content page?
ReplyDeleteMaster 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.
Deletecan we handle event in default web page which event occur in master page
ReplyDeleteWhen requesting a page, content page is loaded first
ReplyDeleteMaster Page file at Applicaiton Lavel:
ReplyDeleteBy including a tag in web.config under system.web tag like this:
Thank You sir for giving valuable information on master pages
ReplyDeleteWhat is the diff b/w User Control & Mater Pages?
ReplyDeleteCan we apply another master's content to the content page of this master page?
ReplyDeletetill 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...
ReplyDeleteCan Someone tell me the answer..
Hi Venkat
ReplyDeletePlease 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
In application one content page and one master page . which is first load..
ReplyDeleteusing page load cycle.
Sequence of execution(Content page,User Control, Master Page)
ReplyDeletesorry my pervious answer is wrong..
ReplyDeleteSandeep 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.
How to edit the master page?
ReplyDelete