天天看點

Webform---母版頁(Master Pages)

母版頁(Master Pages)為網站内的其他頁面提供模版。

Master Page 使您有能力為 web 應用程式中的所有頁面(或頁面組)建立一緻的外觀和行為。

Master Page 為其他頁面提供了模版,帶有共享的布局和功能。Master Page 為内容定義了可被内容頁面覆寫的占位符。而輸出結果就是 Master Page 和内容頁面的組合。

内容頁包含您希望顯示的内容。

當使用者請求内容頁時,ASP.NET 會對頁面進行合并以生成輸出,輸出結果對 Master Page 的布局和内容頁面的内容進行了合并。

=======================================================================================================

導航和頁腳母頁版

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MP1.master.cs" Inherits="MP1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <link href="csss/css1.css" rel="stylesheet" />
    <script src="<%=abc() %>"></script>//解決JS檔案路徑不統一的問題
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
        <div>

            <div id="header">
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </div>

            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            </asp:ContentPlaceHolder>


            <div id="footer"></div>


        </div>
    </form>
</body>
</html>      

MP1.master

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MP1 : System.Web.UI.MasterPage
{
    public void mp1_aaa(string s)
    {
        TextBox1.Text = s;//接收傳值
    }

    public string abc()
    {
        return ResolveClientUrl("js/js1.js");
    }


    protected void Page_Load(object sender, EventArgs e)
    {

    }
}      

MP1.master.cs

嵌入了mp1的左标簽母頁版

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MP2 : System.Web.UI.MasterPage
{
    public void aaa(string s)
    {
        TextBox1.Text = s;

        MP1 mp1 = this.Master as MP1;//傳值
        mp1.mp1_aaa(s);

    }



    protected void Page_Load(object sender, EventArgs e)
    {

    }
}      

MP2.master.cs

<%@ Master Language="C#" MasterPageFile="~/MP1.master" AutoEventWireup="true" CodeFile="MP2.master.cs" Inherits="MP2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">

    <style>
        #left {
            position: relative;
            width: 20%;
            height: 300px;
            background-color: yellow;
            float: left;
        }

        #right {
            position: relative;
            width: 80%;
            height: 300px;
            background-color: aqua;
            float: left;
        }
    </style>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

    <div id="left">
        這裡是标題1<br />
        <br />
        這裡是标題1<br />
        <br />
        這裡是标題1<br />
        <br />
        這裡是标題1<br />
        <br />
        這裡是标題1<br />
        <br />
        這裡是标題1<br />
        <br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    </div>
    <div id="right">

        <asp:ContentPlaceHolder ID="MP2_Content" runat="server"></asp:ContentPlaceHolder>

    </div>
    <div style="clear: both;"></div>
</asp:Content>      

MP2.master

<%@ Page Title="" Language="C#" MasterPageFile="~/MP1.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <h1>這裡是MP1掏出來的第二個頁面</h1>

</asp:Content>      

這裡是MP1掏出來的第二個頁面

<%@ Page Title="" Language="C#" MasterPageFile="~/MP2.master" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MP2_Content" Runat="Server">

    <h1>這裡是MP2逃出來的頁面11111</h1>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" />

</asp:Content>      

這裡是MP2套出來的頁面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;
    }

    void Button1_Click(object sender, EventArgs e)
    {
        //1、把本頁面中的文本框的值取出來
        string s = TextBox1.Text;

        //2、把取出來的值放到母版頁的文本框中去
        MP2 mp2 = this.Master as MP2;
        mp2.aaa(s);

    }
}      

mp2套出來的頁面.cs

轉載于:https://www.cnblogs.com/shadow-wolf/p/6394143.html