---
title: "访问母版页中的标签"
author: "mywebcode"
date: "2012-12-26T21:28:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2013-05-08T10:26:17Z"
id: 1862845
url: "https://mywebcode.iteye.com/blog/1862845"
---

一：在内容页访问模板页  
（1）访问模板页div(要加上runat=server)：1：在母版页添加个div属性（在内容页可以对模板页进行赋值）在模板.cs文件中定义个属性public string DivContent{get{return this.divTile.innertext;}；set{this.divtile.innertext=value}；}  
2:在内容页对应调用的地方（注意：获取的是masterpage属性即定义baodiaomasterpage的父类），所以要在内容页的设计页加上&lt;[%@MasterType](mailto:%@MasterType) VirtualPath="baodiaomaster.marster"%&gt;(指定是baodiaomaster类)  
3：this.Master.属性=“内容”；//设置div内容；this.Master.div的  
id；//获取内容；  
（2）在内容页控制母版页控件this.master.findcontrol("控件id")；  
一延伸：  
在自定义控件中调用页面中控件  
this.Parent.Findcontrol("")  
在页面中获取控件内容  
this.自定义控件名.findcontrol("");  
二：自定义控件  
经常用的东西就可以定义成一个控件；  
1：添加web自定义控件页；  
2：向里面添加控件  
3：用时：&lt;%Register Src="~/MyMenu.ascx" TagName="标签名" TagPrefix=“前缀”%&gt;  
&lt;前缀：标签名 id=“” runat=“”/&gt;