Showing posts with label Master page. Show all posts
Showing posts with label Master page. Show all posts

September 25, 2009

In the previous example -Pass Data from MasterPage to content page -, I showed how to pass data from master page to content page by creating properties in the master page. There, I also mentioned that data can be passed from master to content page using Master.FindControl() method. In this example, I will use this method pass data.

I will use the same master and content page as in the previous example - the only place that needs to change is the Button click event that finds and sets the value of the Label control in the master page.

Here, is the code for the page.




In this event, I am creating a Label control and assigning it to the master page's Label control. Then I am setting the Text property of this Label control to the text entered in the TextBox control.

Data from master page can be passed to content pages either by using Master.FindControl() method on the page or by creating public properties in the master pages. The properties created need to be public, otherwise the page will not be able to access them. In this code snippet, I will create properties in the master page and access them from the content pages. I am also using the same master and content pages as in Create and Use Master Page and have the pages modified to suit this example.

Here, is the code for the masterpage.



The master page, "Content.master", contains a Label control which will be accessed from the content page. The property, "Label1_Data", related to the Label is in the code behind page below.




To use the property "Label1_Data" from the content page, the content page must use "Content.master" as the master page and also declare the MasterType directive and set the virtual path to the path of the master page. Here, is the code of the content page.



The content page contains a Label control to display the data from "Label1_Data" property, a TextBox control that allows "Label1_Data" to be updated, and a Button control to perform postback. Here is the code behind page.



September 22, 2009

Create and Use Master Page

Master page was introduced in Asp.Net 2.0 and defines the layout and markup that is common to all content pages. A
content page is one that is bound to a master page. Creating markup of all the pages in one master pages makes it easy
for designers to modify changes to the layout of a website easily by modifying the master page.

A master page has a .master extension and can contain text, html and server controls. It contains a @Master directive instead of a @Page directive. The default master page (default template created by Visual Studio) includes the html, head and form tags. It also includes one or more ContentPlaceHolder controls to enable content pages to insert content. The master page inherits from the MasterPage class.

To add a master page to a website in Visual Studio, add a new item, and choose the MasterPage. The prompt will look like below.


The default page will look like below.


I will update the master page to have default header and footer. The master page will look below.




Once the master page has been saved, the content pages can be created. Add a new .aspx page by adding new file.




Then choose the master page for the page and click ok.



The resulting page will look below.




Notice that there are 2 content controls, one for head section and one for body section as defined in the master page. To add content to the page, content will need to be added to the content control. Here, I will add a Label control and
some html. The resulting page will look like below.



When the page is run, the page looks like this.



As can be seen, the header and the footer comes from the master page while the second line is generated by the content page.

Reference: Shahed Kazi at AspNetify.com