国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 子长县| 凤城市| 阜康市| 荃湾区| 肃南| 江华| 淮阳县| 太仆寺旗| 二连浩特市| 五寨县| 彭州市| 沿河| 青岛市| 永兴县| 婺源县| 左贡县| 弥勒县| 彰化县| 西充县| 龙泉市| 方正县| 麦盖提县| 兴业县| 新疆| 诏安县| 稷山县| 德钦县| 林甸县| 英德市| 道孚县| 黄山市| 扶绥县| 平阳县| 葫芦岛市| 兴宁市| 远安县| 桑日县| 石楼县| 建始县| 丁青县| 华宁县|