自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET模板控件開(kāi)發(fā)淺析

開(kāi)發(fā) 后端
ASP.NET模板控件那么呢?首先本文從了解ASP.NET模板控件開(kāi)始向你介紹了ASP.NET模板控件的先關(guān)內(nèi)容。

本文主題是ASP.NET模板控件,模板控件將是復(fù)雜控件的起步

ASP.NET模板控件1.ASP.NET內(nèi)置的模板控件,了解模板控件

如下圖,以下為ASP.NET內(nèi)置的模板控件

ASP.NET內(nèi)置的模板控件 

上圖的控件一方面是模板控件,另一方面又是數(shù)據(jù)綁定控件.這里我們暫且不討論如何實(shí)現(xiàn)數(shù)據(jù)綁定.

使用上面控件的話,應(yīng)該熟悉控件存在著不同的模板,如下圖Repeater控件的模板類(lèi)型.

Repeater控件的模板類(lèi)型 

在不同模板內(nèi)你可以定義控件顯示內(nèi)容會(huì)呈現(xiàn)不同效果.典型的運(yùn)用就是GridView,其呈現(xiàn)代碼會(huì)是一個(gè)表格代碼,而Repeater則是自定義的.其實(shí)其是內(nèi)部已經(jīng)實(shí)現(xiàn)了的,暫且先不管這些.下面一步步看下來(lái)如何實(shí)現(xiàn).

ASP.NET模板控件2.實(shí)現(xiàn)模板控件

2.1簡(jiǎn)單實(shí)現(xiàn)模板控件(靜態(tài)模板)

(1)模板控件為特殊的復(fù)合控件,你還是需要實(shí)現(xiàn)INamingContainer接口,因?yàn)樵谀0鍖傩缘膬?nèi)容是為子控件集合添加到模板控件中,為保證控件具有唯一標(biāo)識(shí)符.其實(shí)現(xiàn)將在CreateChildControls方法中創(chuàng)建子控件.

ASP.NET中可以直接繼續(xù)CompositeControl就可

(2)定義控件屬性

模板屬性為System.Web.UI.ITemplate 接口,此接口有一InstantiateIn 方法 將在下面分析

上一篇我們說(shuō)明了控件內(nèi)部屬性和控件的區(qū)別,模板并非控件而是屬性,我們?cè)趯傩詾g覽器中并未看到此屬性,是因?yàn)槲覀優(yōu)槠浼恿嗽獢?shù)據(jù),作為內(nèi)部屬性使用

定義模板屬性方法如下

  1. //聲明變量  
  2. private ITemplate _itemTemplate;  
  3.  
  4.  
  5. //屬性  
  6. [Browsable(false)]  
  7. [TemplateContainer(typeof(Article))]  
  8. [PersistenceMode(PersistenceMode.InnerProperty)]  
  9. public ITemplate ItemTemplate  
  10. {  
  11.     get { return _itemTemplate; }  
  12.     set { _itemTemplate = value; }  

這里我們認(rèn)識(shí)到了一個(gè)TemplateContainer元數(shù)據(jù),其與容器控件關(guān)聯(lián)起來(lái).Article為默認(rèn)其自身控件,即默認(rèn)將自身控件作為容器控件.

(3).重寫(xiě)CreateChildControls方法

此方法我們以前已認(rèn)識(shí)過(guò)了,主要是為控件添加子控件

  1. protected override void CreateChildControls()  
  2. {  
  3.     _itemTemplate.InstantiateIn(this);  

這次我們要做的重點(diǎn)是認(rèn)識(shí)ITemplate接口的InstantiateIn 方法,方法有一個(gè)Control參數(shù),其為子控件和模板定義了一個(gè)容器控件(此處為其自身控件,下面看頁(yè)面代碼).如GridView和DataList控件都實(shí)現(xiàn)了自定義的容器控件.Repeater則是完全自定義的.這里暫且默認(rèn)實(shí)現(xiàn)

ASP.NET模板控件實(shí)現(xiàn)代碼

在模板內(nèi)拖了一個(gè)label控件

  1. ﹤custom:Article  
  2.     id="Article1" 
  3.     Runat="server"﹥  
  4.     ﹤ItemTemplate﹥  
  5.     ﹤asp:Label ID="Label1" runat="server" Text="Label"﹥﹤/asp:Label﹥  
  6.     ﹤/ItemTemplate﹥  
  7. ﹤/custom:Article﹥  

OK,你可以看一下效果了,當(dāng)然你可以定義多個(gè)模板然后在多個(gè)不同模板內(nèi)添加內(nèi)容.我們來(lái)看下其控件樹(shù)內(nèi)容,如下圖

控件樹(shù) 

子控件有一個(gè)Label控件,非控件內(nèi)容則以LiteralControl呈現(xiàn).

2.2實(shí)現(xiàn)動(dòng)態(tài)模板

當(dāng)我們使用DataList控件時(shí),往往在模板中動(dòng)態(tài)的綁定一些數(shù)據(jù),獲取的這些數(shù)據(jù)則是ITemplate接口的InstantiateIn 方法中的容器控件.下面我們?yōu)榭丶x屬性,然后通過(guò)DataBind()方法和數(shù)據(jù)綁定表達(dá)式獲取數(shù)據(jù)

我們先先定義三個(gè)屬性

頁(yè)面代碼,注意要用DataBind()方法

  1. void Page_Load()  
  2. {  
  3.     Article1.Title = "Creating Templated Databound Controls";  
  4.     Article1.Author = "Stephen Walther";  
  5.     Article1.Contents = "Blah, blah, blah, blah";  
  6.     Article1.DataBind();  

通過(guò)Container數(shù)據(jù)綁定表達(dá)式獲取容器對(duì)象屬性,此處容器對(duì)象為默認(rèn)的Article

容器對(duì)象為默認(rèn)的Article 

如下實(shí)現(xiàn)

  1. ﹤custom:Article  
  2.     id="Article1" 
  3.     Runat="server"﹥  
  4.     ﹤ItemTemplate﹥  
  5.     ﹤asp:Label ID="Label1" runat="server" Text="Label"﹥﹤/asp:Label﹥  
  6.     ﹤%# Container.Title%﹥﹤br /﹥  
  7.     ﹤%# Container.Author %﹥﹤br /﹥  
  8.     ﹤%# Container.Contents %﹥﹤br /﹥  
  9.     ﹤/ItemTemplate﹥  
  10. ﹤/custom:Article﹥ 

好了,到這里你就實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的動(dòng)態(tài)模板控件了.

2.3實(shí)現(xiàn)默認(rèn)模板

在購(gòu)書(shū)網(wǎng)站上我們常??吹接捎趫D書(shū)太多的情況下,管理人員未能將圖書(shū)封面發(fā)布到網(wǎng)站上,這時(shí)此書(shū)可能出現(xiàn)默認(rèn)的圖片"尚為此書(shū)添加圖書(shū)封面"

在一個(gè)具有模板的控件里,如果你未為控件添加模板屬性的話,你可以通過(guò)實(shí)現(xiàn)默認(rèn)模板來(lái)實(shí)現(xiàn)默認(rèn)效果.

(1)那你第一步要做的就是定義一個(gè)自定義ASP.NET模板控件.此模板需要實(shí)現(xiàn)ITemplate接口,實(shí)現(xiàn)InstantiateIn方法.看一下典型實(shí)現(xiàn),如下代碼

  1. public class ArticleDefaultTemplate : ITemplate  
  2. {  
  3.     public void InstantiateIn(Control container)  
  4.     {  
  5.         Label lblTitle = new Label();  
  6.         lblTitle.DataBinding += new EventHandler(lblTitle_DataBinding);  
  7.  
  8.         Label lblAuthor = new Label();  
  9.         lblAuthor.DataBinding += new EventHandler(lblAuthor_DataBinding);  
  10.  
  11.         Label lblContents = new Label();  
  12.         lblContents.DataBinding += new EventHandler(lblContents_DataBinding);  
  13.  
  14.         container.Controls.Add(lblTitle);  
  15.         container.Controls.Add(new LiteralControl("﹤br /﹥"));  
  16.         container.Controls.Add(lblAuthor);  
  17.         container.Controls.Add(new LiteralControl("﹤br /﹥"));  
  18.         container.Controls.Add(lblContents);  
  19.     }  
  20.  
  21.     void lblTitle_DataBinding(object sender, EventArgs e)  
  22.     {  
  23.         Label lblTitle = (Label)sender;  
  24.         ArticleWithDefault container = (ArticleWithDefault)lblTitle.NamingContainer;  
  25.         lblTitle.Text = container.Title;  
  26.     }  
  27.  
  28.     void lblAuthor_DataBinding(object sender, EventArgs e)  
  29.     {  
  30.         Label lblAuthor = (Label)sender;  
  31.         ArticleWithDefault container = (ArticleWithDefault)lblAuthor.NamingContainer;  
  32.         lblAuthor.Text = container.Author;  
  33.     }  
  34.  
  35.     void lblContents_DataBinding(object sender, EventArgs e)  
  36.     {  
  37.         Label lblContents = (Label)sender;  
  38.         ArticleWithDefault container = (ArticleWithDefault)lblContents.NamingContainer;  
  39.         lblContents.Text = container.Contents;  
  40.     }  
  41.  

在InstantiateIn方法中,定義了默認(rèn)控件,并實(shí)現(xiàn)了默認(rèn)綁定.在各自的數(shù)據(jù)綁定事件里通過(guò)容器控件(默認(rèn)容器控件為ArticleWithDefault,此處還是沒(méi)自定義容器控件,下面會(huì)介紹)的NamingContainer屬性獲取控件ID值.然后對(duì)控件進(jìn)行賦值.

(2)重寫(xiě)CreateChildControls方法

當(dāng)未定義模板屬性時(shí),則實(shí)現(xiàn)默認(rèn)模板

  1. protected override void CreateChildControls()  
  2. {  
  3.     if (_itemTemplate == null)  
  4.         _itemTemplate = new ArticleDefaultTemplate();  
  5.     _itemTemplate.InstantiateIn(this);  

(3)頁(yè)面代碼

下面實(shí)現(xiàn)效果跟2.2的定義的模板控件效果一樣,這里只為說(shuō)明默認(rèn)模板的使用方法

  1. void Page_Load()  
  2. {  
  3.     ArticleWithDefault1.Title = "Creating Templated Databound Controls";  
  4.     ArticleWithDefault1.Author = "Stephen Walther";  
  5.     ArticleWithDefault1.Contents = "Blah, blah, blah, blah";  
  6.     ArticleWithDefault1.DataBind();  
  7. }  
  8.  
  9. ﹤custom:ArticleWithDefault  
  10.     id="ArticleWithDefault1" 
  11.     Runat="server" /﹥ 

2.4ASP.NET模板控件之實(shí)現(xiàn)自定義容器控件

上面我已經(jīng)多次注明容器控件為默認(rèn)自身控件,你可以通過(guò)自定義容器控件

GridView控件會(huì)自動(dòng)把數(shù)據(jù)以表格形式呈現(xiàn),DataList控件有DataListItem ,Repeater則有RepeaterItem.

這些控件實(shí)現(xiàn)數(shù)據(jù)綁定后,通常不是顯示一條數(shù)據(jù)的,其控件都有一個(gè)Items屬性,其表示項(xiàng)集合.

每項(xiàng)數(shù)據(jù)都在其Item里面,看一下DataList綁定數(shù)據(jù)以后的控件樹(shù)

綁定數(shù)據(jù)以后的控件 

我們常常會(huì)需要在模板控件里以以下方式來(lái)獲取模板內(nèi)部控件

如在DataList控件中

  1. protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)  
  2. {  
  3.     e.Item.FindControl("");  
  4.     DataList1.Items[0].BackColor = System.Drawing.Color.Red;  

通過(guò)此方法我們可以處理一些特殊的列和行.為實(shí)現(xiàn)上面效果,我們也可以為ASP.NET模板控件自定義容器控件

(1)自定義容器控件類(lèi)

注意需要實(shí)現(xiàn)IDataItemContainer接口,就如DataList一樣,其綁定的數(shù)據(jù)不可能是一條的.

  1. public class ProductItem : WebControl, IDataItemContainer  
  2.  {  
  3.      private string _name;  
  4.      private decimal _price;  
  5.  
  6.      public string Name  
  7.      {  
  8.          get { return _name; }  
  9.          set { _name = value; }  
  10.      }  
  11.  
  12.      public decimal Price  
  13.      {  
  14.          get { return _price; }  
  15.          set { _price = value; }  
  16.      }  
  17.  
  18.      public object DataItem  
  19.      {  
  20.          get 
  21.          {  
  22.              return this;  
  23.          }  
  24.      }  
  25.  
  26.      public int DataItemIndex  
  27.      {  
  28.          get { return 0; }  
  29.      }  
  30.  
  31.      public int DisplayIndex  
  32.      {  
  33.          get { return 0; }  
  34.      }  
  35.  } 

然后在主控件中如下實(shí)現(xiàn)

  1. private ProductItem _item;  
  2.  
  3.   public string Name  
  4.   {  
  5.       get 
  6.       {  
  7.           EnsureChildControls();  
  8.           return _item.Name;  
  9.       }  
  10.       set 
  11.       {  
  12.           EnsureChildControls();  
  13.           _item.Name = value;  
  14.       }  
  15.   }  
  16.  
  17.   public Decimal Price  
  18.   {  
  19.       get 
  20.       {  
  21.           EnsureChildControls();  
  22.           return _item.Price;  
  23.       }  
  24.       set 
  25.       {  
  26.           EnsureChildControls();  
  27.           _item.Price = value;  
  28.       }  
  29.   } 

(2)ASP.NET模板控件之用TemplateContainer與模板屬性關(guān)聯(lián)起來(lái)

  1. [TemplateContainer(typeof(ProductItem))]  
  2. [PersistenceMode(PersistenceMode.InnerProperty)]  
  3. public ITemplate ItemTemplate  
  4. {  
  5.     get { return _itemTemplate; }  
  6.     set { _itemTemplate = value; }  

(3)重寫(xiě)CreateChildControls方法

注意了,此處模板的InstantiateIn方法不再是this了,而是自定義容器控件了,再用數(shù)據(jù)綁定表達(dá)式訪問(wèn)的將是ProductItem的數(shù)據(jù)(即自定義容器控件的數(shù)據(jù))

  1. protected override void CreateChildControls()  
  2. {  
  3.     _item = new ProductItem();  
  4.     _itemTemplate.InstantiateIn(_item);  
  5.     Controls.Add(_item);  

(4)頁(yè)面代碼

  1. void Page_Load()  
  2. {  
  3.     Product1.Name = "Laptop Computer";  
  4.     Product1.Price = 1254.12m;  
  5.     Product1.DataBind();  
  6. }  
  7.  
  8. ﹤custom:Product  
  9.     id="Product1" 
  10.     Runat="Server"﹥  
  11.     ﹤ItemTemplate﹥  
  12.      
  13.     Name: ﹤%# Eval("Name") %﹥  
  14.     ﹤br /﹥  
  15.     Price: ﹤%# Eval("Price""{0:c}") %﹥   
  16.     ﹤/ItemTemplate﹥      
  17. ﹤/custom:Product﹥ 

上面以Eval來(lái)綁定數(shù)據(jù),也可以用Container表達(dá)式,如下圖,其類(lèi)型為ProductItem

類(lèi)型為ProductItem 

注意:當(dāng)不是數(shù)據(jù)綁定控件時(shí),則不能用Eval綁定語(yǔ)法,如上面的幾個(gè)例子.大家可以測(cè)試一下.

ASP.NET模板控件的相關(guān)內(nèi)容就向你介紹到這里,希望對(duì)你了解ASP.NET模板控件有所幫助。

【編輯推薦】

  1. ASP.NET控件開(kāi)發(fā)基礎(chǔ)之自定義視圖狀態(tài)管理
  2. ASP.NET控件開(kāi)發(fā)基礎(chǔ)之為子控件添加樣式
  3. ASP.NET控件開(kāi)發(fā)基礎(chǔ)之服務(wù)器控件客戶端功能
  4. ASP.NET控件開(kāi)發(fā)之控件生成器淺析
  5. ASP.NET控件開(kāi)發(fā)基礎(chǔ)的總結(jié)詳解
責(zé)任編輯:仲衡 來(lái)源: 博客園
相關(guān)推薦

2009-08-07 15:34:15

ASP.NET數(shù)據(jù)綁定

2009-07-27 17:25:53

ASP.NET驗(yàn)證控件

2009-08-06 18:18:27

ASP.NET控件開(kāi)發(fā)ASP.NET復(fù)合控件

2009-08-06 15:21:45

ASP.NET控件開(kāi)發(fā)RenderConte

2009-07-28 09:32:41

ASP.NET自定義控

2009-08-07 14:05:21

ASP.NET控件

2009-08-07 17:49:44

控件設(shè)計(jì)器

2009-08-06 13:08:23

ASP.NET控件開(kāi)發(fā)

2009-08-05 18:46:21

ComboBox顯示ASP.NET控件開(kāi)發(fā)

2009-08-07 17:17:43

ASP.NET控件設(shè)計(jì)

2009-08-06 09:18:01

ASP.NET自定義控ASP.NET控件開(kāi)發(fā)

2009-08-07 16:32:52

ASP.NET控件設(shè)計(jì)時(shí)支

2009-08-04 15:20:59

ASP.NET數(shù)據(jù)驗(yàn)證數(shù)據(jù)驗(yàn)證控件

2009-08-07 17:59:35

控件設(shè)計(jì)器

2009-07-24 09:57:25

ASP.NET HTM

2009-08-05 18:32:28

HtmlTextWriASP.NET控件開(kāi)發(fā)

2009-08-06 18:32:00

ASP.NET控件開(kāi)發(fā)ASP.NET復(fù)合控件

2009-08-05 17:11:51

ASP.NET控件開(kāi)發(fā)ASP.NET服務(wù)器控

2009-08-04 10:43:59

ASP.NET控件開(kāi)發(fā)

2009-07-28 16:21:03

Asp.net AjaAutoComplet
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)