System.Web.UI.MasterPage 一点理解

来源:互联网 发布:武林外传电影版 知乎 编辑:程序博客网 时间:2024/05/23 19:00
<!-- 版模页 -->
<%@ Master Language="C#" AutoEventWireup="true" Inherits="DuwControls.DuwDefaultMasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">    
    
<asp:contentplaceholder id="CHead" runat="server">
    
</asp:contentplaceholder>
</head>
<body>
    
<form id="form1" runat="server">
        
<asp:ContentPlaceHolder ID="ScriptMan" runat="server">
               ScriptMain
        
</asp:ContentPlaceHolder>
        
<asp:ContentPlaceHolder ID="CTop" runat="server">
               CTop
        
</asp:ContentPlaceHolder>
        
<asp:ContentPlaceHolder ID="CLeft" runat="server">
               CLeft
        
</asp:ContentPlaceHolder>
        
<asp:ContentPlaceHolder ID="CMain" runat="server">
               CMain
        
</asp:ContentPlaceHolder>
        
<asp:ContentPlaceHolder ID="CRight" runat="server">
               CRight
        
</asp:ContentPlaceHolder>
        
<asp:ContentPlaceHolder ID="CFoot" runat="server">
               CFoot
        
</asp:ContentPlaceHolder>
    
</form>
</body>
</html>
ContentPlaceHolder 为模版部分的占用位置


//模版页后台,继承自系统 System.Web.UI.MasterPage
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web;

namespace DuwControls
{
    
public class DuwDefaultMasterPage : MasterPage
    
{
        
private ContentPlaceHolder _CHead;
        
private ContentPlaceHolder _CTop;
        
private ContentPlaceHolder _CLeft;
        
private ContentPlaceHolder _CMain;
        
private ContentPlaceHolder _CRight;
        
private ContentPlaceHolder _CFoot;
        
private ContentPlaceHolder _ScriptMan;

        
public ContentPlaceHolder CHead get return _CHead; } }
        
public ContentPlaceHolder CTop get return _CTop; } }
        
public ContentPlaceHolder CLeft get return _CLeft; } }
        
public ContentPlaceHolder CMain get return _CMain; } }
        
public ContentPlaceHolder CRight get return _CRight; } }
        
public ContentPlaceHolder CFoot get return _CFoot; } }
        
public ContentPlaceHolder ScriptMan get return _ScriptMan; } }

        
protected override void OnInit(EventArgs e)
        
{
            
foreach (Control ctrl in this.Controls)
            
{
                
if (ctrl is System.Web.UI.HtmlControls.HtmlHead)//模版Head头部部分
                {
                    
foreach (Control ctl in ctrl.Controls)
                    
{
                        
if (ctl is ContentPlaceHolder)
                        
{
                            _CHead 
= ctl as ContentPlaceHolder;
                        }

                    }

                }

                
else if (ctrl is System.Web.UI.HtmlControls.HtmlForm)//模版Form主体部分
                {
                    
foreach (Control ctl in ctrl.Controls)
                    
{
                        
if (ctl is ContentPlaceHolder)
                        
{
                            
switch (ctl.ID)
                            
{
                                
case "CTop":
                                    _CTop 
= ctl as ContentPlaceHolder;
                                    
break;
                                
case "CLeft":
                                    _CLeft 
= ctl as ContentPlaceHolder;
                                    
break;
                                
case "CMain":
                                    _CMain 
= ctl as ContentPlaceHolder;
                                    
break;
                                
case "CRight":
                                    _CRight 
= ctl as ContentPlaceHolder;
                                    
break;
                                
case "CFoot":
                                    _CFoot 
= ctl as ContentPlaceHolder;
                                    
break;
                                
case "ScriptMan":
                                    _ScriptMan 
= ctl as ContentPlaceHolder;
                                    
break;
                                
default:
                                    
break;
                            }

                        }

                    }

                }

            }

            
base.OnInit(e);
        }

    }

}




//页面.cs运用部分
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using DuwCompontents;
using System.Text.RegularExpressions;
using System.IO;
using System.Web;

namespace DuwControls
{
    
public class DuwPage : Page
    
{       
        
属性

        
事件

}




//aspx页面部分
<%@ Page Language="C#" MasterPageFile="~/Themes/Default/Default.master"  ValidateRequest="false"
    Inherits
="DuwControls.DuwPage" SkinType="default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="CHead" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ScriptMan" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="CTop" Runat="Server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="CLeft" Runat="Server">
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="CMain" Runat="Server">      
</asp:Content>
<asp:Content ID="Content6" ContentPlaceHolderID="CRight" Runat="Server">
</asp:Content>
<asp:Content ID="Content7" ContentPlaceHolderID="CFoot" Runat="Server">
</asp:Content>
 
原创粉丝点击