国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

自定義控件(支持模板)

2019-11-17 05:26:31
字體:
供稿:網(wǎng)友
基于模版的簡單控件
<%@ 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();//確定是否包含子控件,否則創(chuàng)建
   base.OnDataBinding (e);
  }  protected override void CreateChildControls()
  {
   if(itemPlate!=null)
   {
    itemPlate.InstantiateIn(this);//當由類實現(xiàn)時,創(chuàng)建子控件對象
   }
   else
   {
     this.Controls.Add(new LiteralControl(" NO TEMPLATE"));
   }
   
  }
  }
}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 阜平县| 安仁县| 车致| 陵川县| 中山市| 鹿泉市| 广东省| 汝城县| 西宁市| 深水埗区| 辛集市| 秭归县| 马公市| 西藏| 中牟县| 安徽省| 偏关县| 镇赉县| 资中县| 乌海市| 吐鲁番市| 综艺| 繁昌县| 卓资县| 天台县| 武隆县| 盐津县| 额济纳旗| 昭平县| 九寨沟县| 黑水县| 青田县| 新和县| 会理县| 永善县| 东源县| 陕西省| 刚察县| 陵川县| 楚雄市| 浙江省|