Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Master Pages

Give us the ability to create a consistent layout for the entire website.
The extension of the master page is : .master
And cannot be run alone ( cannot create an object form)
A content page must inherit from.
Content Place Holder: is the part of the page we can modify in each
page
Make a new website and in website add new item and select master
page and press ok.
Notice in the html source its <%@ Master not @page

And it have acontect place holder tag


We can have many content place holder in the same master page
Go to source and add a label before the placeholder tag
Go to website and new item we will find a new item called add
content page will appear
Now go to content place in the content page and put your cursor inside
and double click a textbox and button

From a content page how to access a control in the master?


1. When button1-click I need to change lblusername text to be equal
to textbox text
See default 2 code
Now a new content page default 3
Go back to to default 2 and double click hyperlink button and in the
property navigate url and choose default 3
Now run page 2 and write something in label and press the hyperlink
It will NOT go to page 3 ( it didn’t maintain the label)
Now we have to restructure the problem so I made a copy of site
See new page 2 code
Then when we load a page there is 2 events occurs
Page pre-init
Page-load
Pre init happens first when we need to change something in the
content of the page we will use pre init not page load
Go to page 3 and double click the page unfortunately we have to write
the pre init code see and code
Run the page 2 and write something in the txt box and press button
and then click the hyperlink will take us to page 3 and the new txt will
appear.
Now we wrote the code again it isn’t good
Now make a new class file from website new item then class
All we do is make class file inherit from master page and make content
pages inherit from class
Make a new copy of project and
In the public class Class1 add :page

Go to page 2 and 3 and make them inherit from class 1


By removing the System.Web.UI.Page and making it class1

And cut and paste the code from page 3 to class1 outside the
constructor with the same pre init function from page 3 
Now its as if the content page have 2 parents
Master page : feed it design
Class 1 : comman functions and code

Another exercise
How to change master page at run time?
We make 2 master pages and put a drop down list to let the user
choose which theme ( thus master page he want)
Now copy the masterpage from solution explorer then right click the
main site node and paste it
Now change the label background and rename it to me colorful
Go to page 3 and put 2 buttons name 1 classic and other colorful
Go to colorful button code
The last event to change the master page is the pre init
And to classic button and code it
( we just put a session with a string colorful or classic)
Now go to class 1 in the pre init event and ask for the session
See the if other if statements
Now clicking the button will make view change but we need 2 clicks to
do that
Because preinit happens then button click then session is created then
on the other pre init it will response
To fix that in the button codes both of them we will reload the page
To do that response redirect the same page we are on
But we wont type the url of the page we are in
Instead use : Request.Url.ToString() inside the response.redirect brackets

You might also like