Small tips: dynamically changing masterpage

Source: Internet
Author: User

In Asp.net 2.0, masterpage is a good thing, but sometimes masterpage needs to be changed dynamically. For example, we often find it in some news sites.
When the "print" button is displayed, some fancy items in the main template will be removed when you press print, such as displaying only the title, date, and footer. At this time,
When the user selects "print", we can dynamically call a new masterpage suitable for printing. For example, suppose there is a display.ArticleDetails page of the content detail. aspx

There is a connection
<Asp: hyperlink id = "print" runat = "server"> Print </ASP: hyperlink>
Write in the server
Public partial class detail: system. Web. UI. Page {

Bool showprintimage = true;
Protected void page_load (Object sender, system. eventargs e ){
If (this. showprintimage ){
This. Print. navigateurl = request. url. tostring () + "& print = true ";
} Else {
This. Print. Visible = false;
}
}
}
In page_load, the connection print is set to print = true by default;
After you click Print connection, to dynamically call the configured masterpage, you must implement it in pre_init. Code
Protected void page_preinit (Object sender, eventargs E)

{
If (request. querystring ["print"]! = NULL ){
This. masterpagefile = "print. Master ";
This. showprintimage = false;
}
}

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.