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;
}
}