当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 自定义控件(可以动态加载用户控件)

ASP.NET
ASP.NET开发:简化应用程序的开发支持Web标准
asp.net XMLHttpRequest实现用户注册前的验证
asp.net 页面间传值方法小结
asp.net url重写浅谈
asp.net 验证码生成和刷新及验证
C#精髓 GridView72大绝技 学习gridview的朋友必看
实例说明asp.net中的简单角色权限控制
asp.net网站开发包wq.dll打包下载
js与ASP.NET 中文乱码问题
asp.net checkbox 动态绑定id GridView删除提示
asp.net TextBox回车触发事件 图片在img显示
asp.net 脏字典过滤问题 用正则表达式来过滤脏数据
asp.NET 脏字过滤算法
asp.NET 脏字过滤算法 修改版
asp.net sql 数据库处理函数命令
asp.net Javascript 的几种写法与提示
ASP.NET MVC学习笔记
asp.net 中国身份证号码验证代码 非正则
Asp.net中使用Sqlite数据库的方法
asp.net 中文字符串提交乱码的解决方法

ASP.NET 中的 自定义控件(可以动态加载用户控件)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 60 ::
收藏到网摘: n/a


自定义控件中动态加载用户控件
自己写个自定义控件,如 要想每次使用Containers时就自动有页眉和页脚,一种方法就是在在做Containers的时候自己在里面加html元素。但是这中方法麻烦,也不容
易便于以后的修改。所以就把页眉和页脚分别做成两个用户控件:Header.ascx,Footer.ascx。这样再在Containers里动态加载他们。Containers的代码:
using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;
namespace WebComponents{ /// /// Containers自定义控件 /// [ParseChildren(true)]//可以创建子控件 public class Containers : Control,INamingContainer { public Containers() { }
private ITemplate itemTemPlate; [TemplateContainer(typeof(Containers))] public ITemplate ItemPlate//项模板 { get{return itemTemPlate;} set{itemTemPlate=value;} }

protected override void CreateChildControls() { this.Controls.Clear(); Control Header=Page.LoadControl("Header.ascx");//动态加载Header.ascx,也可判断何时加载 Controls.Add(Header); Control Footer=Page.LoadControl("Footer.ascx"); Controls.Add(Footer); }
} }
这样你在Header.ascx,Footer.ascx里设置好内容就可以使用。当然也可以缓存起来