小TIPS:動態改變masterpage

來源:互聯網
上載者:User

在asp.net 2.0中,masterpage是個不錯的東西,但有時,要動態更換masterpage的,比如我們常常在一些新聞網站裡,可以
看到有"print"的按鈕,按PRINT的時候,會把主模版的一些花俏的東西去掉,比如只顯示標題,日期,頁尾等,這個時候,
我們可以在當使用者選擇"print"的時候,動態調用一個新的適合列印的masterpage,比如假設有個顯示文章內容的詳細頁detail.aspx

有個串連
 <asp:HyperLink ID="Print" runat="Server" >Print</asp:HyperLink>
則在服務端裡這樣寫
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;
            }
        }
    }
在page_load裡.預設將串連print設定為帶有參數Print=true;
當使用者點選Print串連後,為了動態調用已經設定好的masterpage,要在pre_init裡實現代碼
     protected void Page_PreInit(Object sender, EventArgs e)
 
{
            if (Request.QueryString["Print"] != null) {
                this.MasterPageFile = "print.master";
                this.ShowPrintImage = false;
            }
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.