WebFrom 【主版頁面】

來源:互聯網
上載者:User

標籤:hid   eee   lap   back   round   rom   自動   建立   textbox   

ASP.NET中主版頁面作用

一是提高代碼的複用(把相同的代碼抽出來)

二是使整個網站保持一致的風格和樣式。

  主版頁面存在就一定要有內容頁的存在,否則主版頁面的存在就沒有了意義。

.master

一、添加主版頁面

在建立的主版頁面中你會發現自動產生了兩個ContentPlaceHolder控制項

一個在head區, ID=“head”;

一個在body區,預設ID=“ContentPlaceHolder1”

           --這是兩個預留位置控制項,文本位置預留。

 

二級主版頁面

主版頁面是可以嵌套的,即在原主版頁面的基礎上再建立主版頁面。

嵌套的主版頁面不會自動產生ContentPlaceHolder 控制項,需要手動寫入

 

 一級主版頁面

 

<%@ Master Language="C#" MasterPageFile="~/MP1.master" AutoEventWireup="true" CodeFile="MP2.master.cs" Inherits="MP2" %><asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"></asp:Content>         <%--一級主版頁面預留的坑--%><asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">  <%--一級主版頁面預留的坑--%>    <div style="width: 30%; float: left; height: 500px; background-color: green;">        這是一條菜單選項<br />        <br />        這是一條菜單選項<br />        <br />        這是一條菜單選項<br />        <br />        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>    </div>    <div style="width: 70%; float: left; height: 500px; background-color: yellow;">        <asp:ContentPlaceHolder ID="MP2_Content1" runat="server">        </asp:ContentPlaceHolder>                            <%--二級模板挖的坑,留給後面填--%>    </div>    <div style="clear: both;"></div>    <asp:ContentPlaceHolder ID="mp2_js" runat="server"></asp:ContentPlaceHolder></asp:Content>
View Code

 

三、建立使用主版頁面的Web表單

在建立web表單時,選擇要套用的主版頁面

     1、套用一級模板頁

        在建立的web表單中有兩個Content控制項,與一級主版頁面相對應

 

 

<%@ Page Title="" Language="C#" MasterPageFile="~/MP1.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"></asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">    <h1>這是頁面1,是我們套主版頁面出來的頁面!!!!</h1></asp:Content>
View Code

 

     2、套用二級主版頁面

       在建立Web表單中只有一個Content控制項,自己挖的那個

<%@ Page Title="" Language="C#" MasterPageFile="~/MP2.master" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %><asp:Content ID="Content1" ContentPlaceHolderID="MP2_Content1" Runat="Server">    <h1>這裡是我們套用二級主版頁面MP2出來的頁面啊啊啊!!!!</h1> </asp:Content>
View Code

 

四、主版頁面與子頁之間資料的傳遞:

 在子頁中沒有辦法從外面訪問到主版頁面的內部

可已寫公用方法用調用方法的形式傳值

 

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)    {        string s =TextBox1.Text;       //擷取文字框的值                 Label1.Text = s;        MP2 m2 = this.Master as MP2;   //擷取當前這個頁的主版頁面        m2.aaa(s);                    //調用這個主版頁面的方法    }}
子頁

 

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{    protected void Page_Load(object sender, EventArgs e)    {            }    public void aaa(string a)   //二級母片中的 公用方法    {        TextBox1.Text = a;        // 將傳進來的值賦到 TextBox1 上        MP1 m1 = this.Master as MP1;   //擷取當前這個頁的主版頁面        m1.m1_aaa(a);                  //調用這個網頁的主版頁面的方法    }}
二級主版頁面

 

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{    protected void Page_Load(object sender, EventArgs e)    {    }    public void m1_aaa(string a)    {        TextBox1.Text = a;    //方法    }
一級主版頁面

 

    子頁 —— 二級母片  ——  一級母片     一層層傳遞

 

 

 

 

 

 

 

 

 

 

主版頁面公用的外部樣式表路徑和外部JS檔案的路徑匹配:

WebFrom 【主版頁面】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.