自定义控件(支持模板)
2008-03-08 12:54:23 来源:WEB开发网核心提示:基于模版的简单控件<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApp3.WebForm1" %&g
基于模版的简单控件
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApp3.WebForm1" %>
<%@ Register TagPRefix="MY" NameSpace="WebApp3" Assembly="WebApp3" %>
<!DOCTYPE Html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="javascript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" Runat="Server">
<MY:MyTemplateC Text="button" runat="Server">
<ItemTemplate>
<asp:Button Text="<%# Container.Text %>" Runat=server></asp:Button>
</ItemTemplate>
</MY:MyTemplateC>
</form>
</body>
</HTML>.cs
private 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
{
/// <summary>
/// MyTemplateC 的摘要说明。
/// </summary>
[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"));
}
}
}
}
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApp3.WebForm1" %>
<%@ Register TagPRefix="MY" NameSpace="WebApp3" Assembly="WebApp3" %>
<!DOCTYPE Html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="javascript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" Runat="Server">
<MY:MyTemplateC Text="button" runat="Server">
<ItemTemplate>
<asp:Button Text="<%# Container.Text %>" Runat=server></asp:Button>
</ItemTemplate>
</MY:MyTemplateC>
</form>
</body>
</HTML>.cs
private 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
{
/// <summary>
/// MyTemplateC 的摘要说明。
/// </summary>
[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"));
}
}
}
}
- ››控件一:Gallery 之无限循环的问题
- ››控件一:Gallery 之基础用法
- ››定义名称时自动代码提示所需的后缀
- ››支持 Symbian 平台的开发者只有四分之一
- ››支持电骡视频点播 迅雷泛播放体验
- ››支持Win 7 Vista 不开迅雷接管高速下载
- ››支持换肤功能的窗口实例
- ››支持iPhone的免费VPN:VPN98
- ››支持PDF阅读!美图看看新版试用
- ››支持Windows CE和Android 2.1双系统的Viliv Prime...
- ››支持Flash Windows Mobile 手机全功能浏览器
- ››支持 Wi-Fi MOTO 第9款 Android 手机ME508曝光
更多精彩
赞助商链接