日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
ASP.NET模板控件開發(fā)淺析

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

創(chuàng)新互聯(lián)于2013年開始,先為海勃灣等服務(wù)建站,海勃灣等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為海勃灣企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

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

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

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

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

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

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

定義模板屬性方法如下

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

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

(3).重寫CreateChildControls方法

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

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

這次我們要做的重點(diǎn)是認(rèn)識(shí)ITemplate接口的InstantiateIn 方法,方法有一個(gè)Control參數(shù),其為子控件和模板定義了一個(gè)容器控件(此處為其自身控件,下面看頁面代碼).如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)容.我們來看下其控件樹內(nèi)容,如下圖

子控件有一個(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屬性,然后通過DataBind()方法和數(shù)據(jù)綁定表達(dá)式獲取數(shù)據(jù)

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

頁面代碼,注意要用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();
  7. }

通過Container數(shù)據(jù)綁定表達(dá)式獲取容器對(duì)象屬性,此處容器對(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)模板

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

在一個(gè)具有模板的控件里,如果你未為控件添加模板屬性的話,你可以通過實(shí)現(xiàn)默認(rèn)模板來實(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.         Label lblAuthor = new Label();
  8.         lblAuthor.DataBinding += new EventHandler(lblAuthor_DataBinding);
  9.         Label lblContents = new Label();
  10.         lblContents.DataBinding += new EventHandler(lblContents_DataBinding);
  11.         container.Controls.Add(lblTitle);
  12.         container.Controls.Add(new LiteralControl("﹤br /﹥"));
  13.         container.Controls.Add(lblAuthor);
  14.         container.Controls.Add(new LiteralControl("﹤br /﹥"));
  15.         container.Controls.Add(lblContents);
  16.     }
  17.     void lblTitle_DataBinding(object sender, EventArgs e)
  18.     {
  19.         Label lblTitle = (Label)sender;
  20.         ArticleWithDefault container = (ArticleWithDefault)lblTitle.NamingContainer;
  21.         lblTitle.Text = container.Title;
  22.     }
  23.     void lblAuthor_DataBinding(object sender, EventArgs e)
  24.     {
  25.         Label lblAuthor = (Label)sender;
  26.         ArticleWithDefault container = (ArticleWithDefault)lblAuthor.NamingContainer;
  27.         lblAuthor.Text = container.Author;
  28.     }
  29.     void lblContents_DataBinding(object sender, EventArgs e)
  30.     {
  31.         Label lblContents = (Label)sender;
  32.         ArticleWithDefault container = (ArticleWithDefault)lblContents.NamingContainer;
  33.         lblContents.Text = container.Contents;
  34.     }
  35. }

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

(2)重寫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);
  6. }

(3)頁面代碼

下面實(shí)現(xiàn)效果跟2.2的定義的模板控件效果一樣,這里只為說明默認(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. ﹤custom:ArticleWithDefault
  9.     id="ArticleWithDefault1"
  10.     Runat="server" /﹥

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

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

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ù)以后的控件樹

我們常常會(huì)需要在模板控件里以以下方式來獲取模板內(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;
  5. }

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

(1)自定義容器控件類

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

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

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

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

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

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

(3)重寫CreateChildControls方法

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

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

(4)頁面代碼

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

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

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

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


本文題目:ASP.NET模板控件開發(fā)淺析
URL標(biāo)題:http://m.5511xx.com/article/ccopedj.html