当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 自定义控件(支持模板)

ASP.NET
利用ASP.NET和AJAX解决手工拼接HTML问题
Asp.net关于动态输出服务器控件的应用
技巧/诀窍:在ASP.NET中重写URL
ASP.NET 自定义控件从入门到精通3
以Post方式向网页发送数据
ASP.NET实现数据采集
使用ASP.NET Image Generation生成图片缩略图及水印
ASP.NET安全问题--ASP.NET安全架构
反思软件系统与软件系统之间的集成交互问题
.Net实现程序的插件机制
作为ASP.NET开发人员必须养成的编程习惯
总结了一下ADO.NET数据库连接的相关知识
VB.NET中有用的通用对象列表
HTTP Error 503与.NET 3.5 SP1 X64
ASP.NET创建Web服务之使用事务
ASP.NET中基类Page_Load方法后执行原因分析
ASP.NET中让网页弹出窗口不再困难
改变.net网站的默认解决方案位置
.net垃圾回收和CLR 4.0对垃圾回收所做的改进之二
.net垃圾回收和CLR 4.0对垃圾回收所做的改进之一

ASP.NET 中的 自定义控件(支持模板)


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


基于模版的简单控件<%@ Page language= c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApp3.WebForm1" %><%@ Register TagPrefix= NameSpace="WebApp3" Assembly="WebApp3" %> WebForm1
" Runat=server>


.csprivate void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 if(!this.Page.IsPostBack) DataBind(); }
控件代码:
using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Drawing;using System.Web;using System.Web.SessionState;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;
namespace WebApp3{ /// /// MyTemplateC 的摘要说明。 /// [ParseChildren(true)] public class MyTemplateC: Control,INamingContainer { private ITemplate itemPlate; [TemplateContainer(typeof(MyTemplateC))]//指定当前控件类型 public ITemplate ItemTemplate { get{return itemPlate;} set{itemPlate=value;} }
private string text; public string Text { get{return text;} set{text=value;} }
protected override void OnDataBinding(EventArgs e) { this.EnsureChildControls();//确定是否包含子控件,否则创建 base.OnDataBinding (e); }
protected override void CreateChildControls() { if(itemPlate!=null) { itemPlate.InstantiateIn(this);//当由类实现时,创建子控件对象 } else { this.Controls.Add(new LiteralControl(" NO TEMPLATE")); } } }}