Asp. NET to dynamically load master pages

Source: Internet
Author: User

In project development, sometimes applications want to be able to dynamically set up master pages based on specific scenarios. There are two scenarios in which you need to dynamically load Master Pages: Allow users to customize appearance rendering with different master pages, and when working with other companies, you need to adjust the appearance of the page to the partner.

Developers can use the MasterPageFile property provided by the page class to set the master page for a content page. Master pages and content pages are merged early in the page declaration cycle. Therefore, you cannot set the master page directly in the Page_Load event of the content page. Instead, you need to dynamically load the master page in the page.preinit event (page.preinit is the first event in the page execution cycle).

  

The following example shows how to dynamically load a master page.

(1) Create a Web site named "Dynamically loading Master Pages".

(2) Add a stylesheet file Stylesheet.css to the Web site, with the following specific code:

(3) Create three master pages, named "Masterpage.master", "Masterpage2.master", and "Masterpage3.master", respectively. The code for the master page Masterpage.master is as follows:

Master Pages Masterpage2.master and masterpage3.master are similar to Masterpage.master. The difference is in "Hello, welcome!" "(Master Page 2)" and "(Master Page 3)" were added after the text.

(4) Add a content page named "Default.aspx". This content page applies the master page masterpage.master. Add two hyperlinks to the content on the right side of the contents page, specifying two different master pages based on the value of the query string. The contents of Default.aspx are as follows:

Add the following code to the Page_PreInit event of the content page to get the master page selected by the user and to switch dynamically. The specific code is as follows:

Run Results 1, 2, 3, as shown.

Figure 1 Run result diagram 2 run result map 3 run result graph

  

In this example, a master page file is assigned to the MasterPageFile property of the page class in the Page_PreInit event of the content page. The master page can be switched dynamically at run time based on the user's choice.

"Reprint" http://rzchina.blog.163.com/blog/static/181105197201321393214665/

Asp. NET to dynamically load master pages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.