|By Jeevan Murkoth||
|December 7, 2004 12:00 AM EST||
Since the advent of Web development, Web developers have sought a way to provide a consistent look and feel across the Web application. This pursuit has resulted in different custom solutions. With the introduction of master pages in ASP.NET 2.0, Microsoft has finally brought the support to the framework level and has given the developers what they have been asking for. Master pages, as the name suggests, allow the developer to define the layout in a single page and easily apply the layout to multiple pages across the Web site, thereby ensuring a consistent appearance. This article looks at how we use master pages with the help of some examples. We will also look at some of the new features that are available in ASP.NET 2.0.
Master pages allow the developer to take a compartmentalized approach while developing Web sites. It allows the developer to move standardized layouts like header, footer, navigation, or any other content that is common over multiple pages to the master page. Content that is specific to a particular page can be defined in content pages that we will see in the next section. Master pages are transparent to the users and when a request is made to the URL of the content page, ASP.NET merges the content of the content page with that of the master page and renders the resulting page. Unlike ordinary ASPX pages, a Web server does not serve a master page and if a user makes a request to the master page, the server returns an error message (see Figure 1).
Master pages are like any other ASP.NET pages with a couple of exceptions. They are identified by a .master file extension. Listing 1 (the code is online at www.sys-con.com/dotnet/source.cfm) is an example of a simple master page. Note that instead of the familiar page directive <[email protected]%> in a normal ASPX page, the master page has a <[email protected] %> directive. Master pages define ContentPlaceholder controls that act as placeholders where content from the content page will be inserted. ContentPlaceholder control is specific to master pages and cannot be declared in a content page. In this example we have defined one ContentPlaceholder control identified as ContentPlaceholder1. A master page can define zero to any number of ContentPlaceholder controls.
The content page is the actual page that a user will request from the server. Content pages are like typical ASPX pages and have a .ASPX extension. However, they differ from an ordinary ASPX page in two ways. The first difference is that a content page has to be associated with a master page, which is done by setting the MasterPageFile attribute in the page directive. Second, all of the content in a Web page must be defined inside a new content control. Any content that is defined outside the content control results in an error. The content control in turn has to be mapped to the Contentplaceholder controls that have been defined in the Master page. Listing 2 shows a content page that refers to Sample.master as its master page. In this example the content page defines a content control called Content1. This content control in turn is mapped to the ContentPlaceholder ContentPlaceHolder1 of the master page by setting the ContentPlaceHolderID.
Sequence of Events
Just like regular ASPX pages, master pages and content pages have events that are fired during the page life cycle. They can also have user controls that need to be initialized. The list below shows the sequence of events in a content page that references a master page.
- Controls in the master page are initialized
- Controls in the content page are initialized
- Master page is initialized
- Content page is initialized
- Content page loads
- Master page loads
- Master page controls load
- Content page controls load
Now that we have seen what a master page and a content page are, let's see how to create a master and content page using Visual Studio .NET 2005 Beta. Visual Studio allows us to add master pages by choosing an item of type Master Page in the Add New Item option. It also gives us the option to have a code behind page associated with the master page and also select the language of the page. In Figure 2, I add a master page called MyMaster1.master using the Add New Item dialog.
To the master page that I just created, I have added an image, footer, and two content placeholders. Each of the ContentPlaceholder controls that has been added has a unique ID which will be referred to by the content page to insert its content. Listing 3 shows the markup and Figure 3 shows the design view of the master page that I have created. Any content page that uses this master page will automatically inherit the image, the heading, and the footer.
Now that we have created the master page, let's go ahead and create a content page using the Add New Item option in Visual Studio .NET 2005 Beta (see Figure 4). A content page is just an ordinary Web form and Visual Studio .NET 2005 Beta does not have a special type called content page. When we create the content page, the Add New Item dialog, however, gives us the option to select the master page that will be referenced by the Web form. Once we select this option, Visual Studio presents us with a dialog box that lists all the master pages in the Web site (see Figure 5). Figure 6 shows the design view of the content page that we have just created. You will notice that all the master page content is grayed out and cannot be edited. Listing 4 shows the markup of the content page we just created. The only editable portion in the content page is between the <asp:Content> tags. Each of these content tags is linked to ContentPlaceholder of the master page by setting the ContentPlaceHolderID for the content control. In Listing 4, Content1 is mapped to ContentPlaceholder1 of the master page and Content2 is mapped to ContentPlaceholder2 of the master page. Content control in turn can contain other controls. In this example I have added some text in each of the content tags. Figure 7 shows the browser rendering of the content page.
Nesting Master Pages
Master pages can be nested and can have one master page refer to another as its master. Nested master pages are very useful while designing Web sites with different sections where each individual section has to have its own layout and at the same time be consistent with the overall appearance of the Web site. For example, in a corporate Web site we could have a master page that could define the global menu and a master page for each department that would define the departmental menu. Nesting the department master page and the corporate master page would allow us to combine the two menus. Visual Studio .NET 2005 Beta, however, does not provide designer support for nested master pages.
Let's look at how to create a nested master page with the help of an example. Listing 5 shows the parent master page, ParentMaster.master. This parent master page is then referred to by the child master page (see Listing 6). As you can see, the child master page is not very different from the parent master page. They have the same .master extension; however, the child master page has a content control that is mapped to the ContentPlaceHolder control defined in the parent master page. The child master page can also define ContentPlaceHolder controls of its own to display content of the content page that refers to this page as its master. The child master page in our example defines a content control that is mapped to ContentPlaceHolder1 of the master page. It defines a Contentplaceholder control of its own ChildContentPlaceHolder1 to which the content page will map its content. Listing 7 is the content page that refers to the child master page as its master page. Figure 8 shows the rendering of the content page that uses nested master pages.
Configuring a Site to Use Master Pages
Instead of giving the master attribute in the page directive of each and every page in a Website, ASP.NET 2.0 gives you the option to set the master page in the Web.config file. This is very useful in large Web sites as it gives the developers the flexibility to make the change at one place and apply it across the site. The code snippet below shows how to set the master page in the web.config file:
<pages masterPageFile="MyMaster1.master" />
A couple of things need to be pointed out about setting master pages through the web.config file. First, if the content page has a master page explicitly set in its page directive, that master page assumes precedence over the one set in the web.config file. If there are multiple master page settings in different web.config files, the master page setting in the immediate folder overrides the other setting similar to how in an application with multiple sub- folders, the setting of the web config in the immediate folder takes precedence over the others.
Accessing Master Page Controls from Content Pages
The developer may need to access the controls in the master page for many reasons. One could be to display a value that is relevant to the content page. ASP.NET 2.0 gives you multiple options to achieve this. The first step is to get a reference to the master page. In a content page, the Master property returns the reference to the master page. Once we have obtained the reference to the master page, it is now possible to refer to the controls of the master page. We will look at two approaches. The first approach is early binding, where the control is declared as a public property in the master page. Listing 8 shows the master page where we first expose the text of control Label2 as a public property LabelText. We then access and set the value of LabelText in the page load event of the content page as shown in Listing 9. Another thing to point out is the strong typed reference to the Master page using the <[email protected]%> directive in the Content page. This explicitly casts the value of the master property to the master page object which in our case is AccessMasterProperties.
The second approach is the late binding approach. Here we make use of the familiar FindControl method. We get a reference to a label control in the master page and change the text value from the content page. We search for the label by its ID which in this case is Label1 and then cast it to a label control and set the value for the text.
Dynamically Changing Master Pages
ASP.NET 2.0 allows the developer to change the master page associated with a content page dynamically at run-time. This is very useful if the developer would like to give the users the flexibility of choosing the master pages. The master page for a content page can be set programmatically by assigning a value to the MasterPagefile property of the page. However, this dynamic assignment can be done only in the preinit event of the content page since this is the earliest event that occurs in the page execution life cycle. If we attempt to set the master page file in events like pageload, it would result in an exception. Listing 10 shows an example where we have a dropdown list with the available master pages. On clicking the button we redirect the response to the same page and pass the value of the master page to be set as a request string. The MasterPagefile is then set in the preinit event.
Device-Specific Master Pages
ASP.NET 2.0 pages and controls have built-in support that enables them to automatically render appropriate content depending on the browser and the device that has requested the page. Master pages also support similar behavior. We can create a master page specifically for a device or a browser and in the content page instead of binding to a single master page. We can bind them to number of master pages, each of them specific to a particular device. The binding is qualified with the device name. Listing 11 shows a content page that is bound to two master pages, one for Internet explorer and another for mozilla. The Internet Explorer master page is prefixed with ie and the one for Mozilla is prefixed with Mozilla. These browser prefixes are defined in the browsercaps section of the machine.config file. Depending upon the browser requesting the page, the content page is bound to the appropriate master page. Figures 9 and 10 show the rendering of the content page when it is requested through the two different browsers, Mozilla and Internet Explorer. As you see, ASP.NET 2.0 has automatically chosen the appropriate master page.
In this article we looked at the concept of master pages and saw how they allow the developer to easily create reusable page layouts. It also greatly simplifies maintenance as it is now possible to make changes at one place and propagate them throughout the Web site. I am sure that this feature will have a significant impact on how ASP.NET applications are built in the future.
|Mujtaba Syed 12/14/04 03:48:48 PM EST|
Akana has announced the availability of version 8 of its API Management solution. The Akana Platform provides an end-to-end API Management solution for designing, implementing, securing, managing, monitoring, and publishing APIs. It is available as a SaaS platform, on-premises, and as a hybrid deployment. Version 8 introduces a lot of new functionality, all aimed at offering customers the richest API Management capabilities in a way that is easier than ever for API and app developers to use.
Aug. 25, 2016 06:00 AM EDT Reads: 1,415
DevOps at Cloud Expo, taking place Nov 1-3, 2016, at the Santa Clara Convention Center in Santa Clara, CA, is co-located with 19th Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait for long dev...
Aug. 25, 2016 02:30 AM EDT Reads: 2,144
Is the ongoing quest for agility in the data center forcing you to evaluate how to be a part of infrastructure automation efforts? As organizations evolve toward bimodal IT operations, they are embracing new service delivery models and leveraging virtualization to increase infrastructure agility. Therefore, the network must evolve in parallel to become equally agile. Read this essential piece of Gartner research for recommendations on achieving greater agility.
Aug. 25, 2016 02:15 AM EDT Reads: 476
Personalization has long been the holy grail of marketing. Simply stated, communicate the most relevant offer to the right person and you will increase sales. To achieve this, you must understand the individual. Consequently, digital marketers developed many ways to gather and leverage customer information to deliver targeted experiences. In his session at @ThingsExpo, Lou Casal, Founder and Principal Consultant at Practicala, discussed how the Internet of Things (IoT) has accelerated our abil...
Aug. 25, 2016 02:00 AM EDT Reads: 1,895
SYS-CON Events announced today that 910Telecom will exhibit at the 19th International Cloud Expo, which will take place on November 1–3, 2016, at the Santa Clara Convention Center in Santa Clara, CA. Housed in the classic Denver Gas & Electric Building, 910 15th St., 910Telecom is a carrier-neutral telecom hotel located in the heart of Denver. Adjacent to CenturyLink, AT&T, and Denver Main, 910Telecom offers connectivity to all major carriers, Internet service providers, Internet backbones and ...
Aug. 25, 2016 02:00 AM EDT Reads: 1,783
With so much going on in this space you could be forgiven for thinking you were always working with yesterday’s technologies. So much change, so quickly. What do you do if you have to build a solution from the ground up that is expected to live in the field for at least 5-10 years? This is the challenge we faced when we looked to refresh our existing 10-year-old custom hardware stack to measure the fullness of trash cans and compactors.
Aug. 25, 2016 01:15 AM EDT Reads: 1,607
The emerging Internet of Everything creates tremendous new opportunities for customer engagement and business model innovation. However, enterprises must overcome a number of critical challenges to bring these new solutions to market. In his session at @ThingsExpo, Michael Martin, CTO/CIO at nfrastructure, outlined these key challenges and recommended approaches for overcoming them to achieve speed and agility in the design, development and implementation of Internet of Everything solutions wi...
Aug. 25, 2016 12:45 AM EDT Reads: 1,904
19th Cloud Expo, taking place November 1-3, 2016, at the Santa Clara Convention Center in Santa Clara, CA, will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud strategy. Meanwhile, 94% of enterpri...
Aug. 25, 2016 12:00 AM EDT Reads: 2,977
Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more business becomes digital the more stakeholders are interested in this data including how it relates to business. Some of these people have never used a monitoring tool before. They have a question on their mind like “How is my application doing” but no id...
Aug. 24, 2016 09:15 PM EDT Reads: 1,675
SYS-CON Events announced today that Venafi, the Immune System for the Internet™ and the leading provider of Next Generation Trust Protection, will exhibit at @DevOpsSummit at 19th International Cloud Expo, which will take place on November 1–3, 2016, at the Santa Clara Convention Center in Santa Clara, CA. Venafi is the Immune System for the Internet™ that protects the foundation of all cybersecurity – cryptographic keys and digital certificates – so they can’t be misused by bad guys in attacks...
Aug. 24, 2016 04:15 PM EDT Reads: 2,562
Pulzze Systems was happy to participate in such a premier event and thankful to be receiving the winning investment and global network support from G-Startup Worldwide. It is an exciting time for Pulzze to showcase the effectiveness of innovative technologies and enable them to make the world smarter and better. The reputable contest is held to identify promising startups around the globe that are assured to change the world through their innovative products and disruptive technologies. There w...
Aug. 24, 2016 04:15 PM EDT Reads: 381
SYS-CON Events announced today Telecom Reseller has been named “Media Sponsor” of SYS-CON's 19th International Cloud Expo, which will take place on November 1–3, 2016, at the Santa Clara Convention Center in Santa Clara, CA. Telecom Reseller reports on Unified Communications, UCaaS, BPaaS for enterprise and SMBs. They report extensively on both customer premises based solutions such as IP-PBX as well as cloud based and hosted platforms.
Aug. 24, 2016 04:15 PM EDT Reads: 514
Smart Cities are here to stay, but for their promise to be delivered, the data they produce must not be put in new siloes. In his session at @ThingsExpo, Mathias Herberts, Co-founder and CTO of Cityzen Data, will deep dive into best practices that will ensure a successful smart city journey.
Aug. 24, 2016 02:15 PM EDT Reads: 1,446
The 19th International Cloud Expo has announced that its Call for Papers is open. Cloud Expo, to be held November 1-3, 2016, at the Santa Clara Convention Center in Santa Clara, CA, brings together Cloud Computing, Big Data, Internet of Things, DevOps, Digital Transformation, Microservices and WebRTC to one location. With cloud computing driving a higher percentage of enterprise IT budgets every year, it becomes increasingly important to plant your flag in this fast-expanding business opportuni...
Aug. 24, 2016 12:00 PM EDT Reads: 3,855
In today's uber-connected, consumer-centric, cloud-enabled, insights-driven, multi-device, global world, the focus of solutions has shifted from the product that is sold to the person who is buying the product or service. Enterprises have rebranded their business around the consumers of their products. The buyer is the person and the focus is not on the offering. The person is connected through multiple devices, wearables, at home, on the road, and in multiple locations, sometimes simultaneously...
Aug. 24, 2016 09:45 AM EDT Reads: 2,260
Internet of @ThingsExpo, taking place November 1-3, 2016, at the Santa Clara Convention Center in Santa Clara, CA, is co-located with 19th Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The Internet of Things (IoT) is the most profound change in personal and enterprise IT since the creation of the Worldwide Web more than 20 years ago. All major researchers estimate there will be tens of billions devices - comp...
Aug. 24, 2016 09:00 AM EDT Reads: 3,519
For basic one-to-one voice or video calling solutions, WebRTC has proven to be a very powerful technology. Although WebRTC’s core functionality is to provide secure, real-time p2p media streaming, leveraging native platform features and server-side components brings up new communication capabilities for web and native mobile applications, allowing for advanced multi-user use cases such as video broadcasting, conferencing, and media recording.
Aug. 24, 2016 07:45 AM EDT Reads: 2,098
Data is the fuel that drives the machine learning algorithmic engines and ultimately provides the business value. In his session at Cloud Expo, Ed Featherston, a director and senior enterprise architect at Collaborative Consulting, will discuss the key considerations around quality, volume, timeliness, and pedigree that must be dealt with in order to properly fuel that engine.
Aug. 24, 2016 07:15 AM EDT Reads: 1,668
Amazon has gradually rolled out parts of its IoT offerings in the last year, but these are just the tip of the iceberg. In addition to optimizing their back-end AWS offerings, Amazon is laying the ground work to be a major force in IoT – especially in the connected home and office. Amazon is extending its reach by building on its dominant Cloud IoT platform, its Dash Button strategy, recently announced Replenishment Services, the Echo/Alexa voice recognition control platform, the 6-7 strategic...
Aug. 24, 2016 04:30 AM EDT Reads: 2,191
Cloud computing is being adopted in one form or another by 94% of enterprises today. Tens of billions of new devices are being connected to The Internet of Things. And Big Data is driving this bus. An exponential increase is expected in the amount of information being processed, managed, analyzed, and acted upon by enterprise IT. This amazing is not part of some distant future - it is happening today. One report shows a 650% increase in enterprise data by 2020. Other estimates are even higher....
Aug. 24, 2016 12:00 AM EDT Reads: 2,859