ASP.NET中实现模版的动态加载
ASP.NET中,經(jīng)常會(huì)使用到templates(模版)功能,比如在datagrid,datalist,repeater等控件中,使用templates,將會(huì)大大增強(qiáng)其功能。以往,我們一般是在設(shè)計(jì)程序時(shí),就已經(jīng)設(shè)置好控件中的模版是怎樣的了。但是,有的時(shí)候,可能我們需要?jiǎng)討B(tài)加載模版,比如,當(dāng)你要求你的應(yīng)用程序的界面風(fēng)格隨著用戶的需求而變化時(shí),你就需要到動(dòng)態(tài)加載模版的功能了。但要注意的是,并不是所有的web控件都支持模版功能,而且要注意,哪些控件支持模版的哪些功能,下面簡單列出了一些支持模版功能的控件:
Repeater控件,支持的模版有:
HeaderTemplate, FooterTemplate, ItemTemplate, AlternatingItemTemplate, SeperatorTemplate.
Datelist控件,支持的模版有:
HeaderTemplate, FooterTemplate, ItemTemplate, AlternatingItemTemplate, SeparatorTemplate, SelectedItemTemplate, EditItemTemplate.
Datagrid控件,支持的模版有:
HeaderTemplate, FooterTemplate, ItemTemplate, EditItemTemplate, Pager.
下面,我將以動(dòng)態(tài)加載datalist控件的模版來說明如何動(dòng)態(tài)加載模版:
首先來了解動(dòng)態(tài)加載模版的原理。在.NET中,有templatecontrol類,這個(gè)類是page和usercontrol類的基類。它也同時(shí)定義了page和usercontrol類的基本功能。該類提供了兩個(gè)方法:loadcontrol和loadtemplate。Loadcontrol方法裝載來自外部文件的控件,并且返回usercontrol類對象。而loadtemplate方法加載來自外部文件的模版并且返回的是Itemplate對象。
Loadtemplate方法中,只有一個(gè)參數(shù),參數(shù)值是外部模版文件的路徑,并且返回itemplate對象。而datalist控件提供了一系列的屬性,可以設(shè)置各種模版的屬性,包括有AlternatingItemTemplate, EditItemTemplate, FooterTemplate, HeaderTemplate, ItemTemplate, SelectedItemTemplate, 和 SeperatorTemplate,在下文中,將會(huì)看到相關(guān)介紹。
接著,我們開始介紹例子,在示例程序中,是使用動(dòng)態(tài)創(chuàng)建數(shù)據(jù)表和數(shù)據(jù)列的,并且將數(shù)據(jù)的創(chuàng)建封裝到一個(gè)Db類中,好讓讀者進(jìn)一步回顧如何動(dòng)態(tài)創(chuàng)建數(shù)據(jù)表,數(shù)據(jù)列等,并沒用從數(shù)據(jù)庫中提取(當(dāng)然,你也可以用傳統(tǒng)的讀取數(shù)據(jù)庫的方法),
| 以下為引用的內(nèi)容: public class DB{public DB(){ }/// <summary>/// Method returns a DataSet object filled with data/// </summary>public static DataSet GetDataSet(){//創(chuàng)建dataset和datatableDataSet ds = new DataSet();DataTable table = new DataTable("Records");DataColumn col;//增加一個(gè)列col = new DataColumn();col.DataType = System.Type.GetType("System.Int32");col.ColumnName = "ID";col.ReadOnly = true;col.Unique = true;table.Columns.Add(col);col = new DataColumn();col.DataType = System.Type.GetType("System.String");col.ColumnName = "Name";col.AutoIncrement = false;col.Caption = "Name";col.ReadOnly = false;col.Unique = false;table.Columns.Add(col);col = new DataColumn();col.DataType = System.Type.GetType("System.String");col.ColumnName = "Address";col.AutoIncrement = false;col.Caption = "Address";col.ReadOnly = false;col.Unique = false;table.Columns.Add(col);//增加一條記錄DataRow row = table.NewRow();row["ID"] = 1001;row["Name"] = "Melanie Giard";row["Address"] = "23rd Street, Park Road, NY City, NY";table.Rows.Add(row);row = table.NewRow();row["ID"] = 1002;row["Name"] = "Puneet Nehra";row["Address"] = "3rd Blvd, Ashok Vihar, New Delhi";table.Rows.Add(row);row = table.NewRow();row["ID"] = 1003;row["Name"] = "Raj Mehta";row["Address"] = "Nagrath Chowk, Jabalpur";table.Rows.Add(row);row = table.NewRow();row["ID"] = 1004;row["Name"] = "Max Muller";row["Address"] = "25 North Street, Hernigton, Russia";table.Rows.Add(row);// Add DataTable to DataSetds.Tables.Add(table);// Return DataSetreturn ds;}} |
接下來,我們首先創(chuàng)建若干個(gè)模版文件。我們先創(chuàng)建兩組模版文件,每一組模版文件分別包含有header,footer,item,alternating item四個(gè)模版文件,保存成.ascx文件,這樣,我們就有兩類型風(fēng)格的模版了,每類型風(fēng)格的模版中都有自己的header,footer,item,alternating item子模版。下面為其中一個(gè)item模版文件,其他的類似。
| 以下為引用的內(nèi)容: <%@ Control Language="VB" %><FONT face="verdana" color="green" size="2"><b>ID: </b><%# DataBinder.Eval(CType(Container, DataListItem).DataItem, "ID") %><b>Name: </b><%# DataBinder.Eval(CType(Container, DataListItem).DataItem, "Name") %><br><b>Address: </b><%# DataBinder.Eval(CType(Container, DataListItem).DataItem, "Address") %><p></FONT> |
最后,我們開始創(chuàng)建應(yīng)用程序,新建一個(gè)工程,添加兩個(gè)按鈕和一個(gè)datalist控件如下圖:
之后創(chuàng)建一個(gè)binddatagrid的方法,將dataset綁定到datalist控件中去,代碼如下:
| 以下為引用的內(nèi)容: private void BindDataGrid(){dtSet = DB.GetDataSet();DataList1.DataSource = dtSet.Tables[0].DefaultView;DataList1.DataBind();}private void Page_Load(object sender, System.EventArgs e){if(!IsPostBack){BindDataGrid();}}? |
最后,分別為兩個(gè)按鈕的clcik事件添加代碼,分別使用page.loadtemplate方法去加載我們已經(jīng)寫好的兩套模版組中的模版,代碼如下:
| 以下為引用的內(nèi)容: private void Button1_Click(object sender, System.EventArgs e){// Load templatesDataList1.AlternatingItemTemplate =Page.LoadTemplate("AltItemTempate.ascx");DataList1.ItemTemplate =Page.LoadTemplate("ItemTemplate.ascx");DataList1.HeaderTemplate =Page.LoadTemplate("HeadTemplate.ascx");DataList1.FooterTemplate = Page.LoadTemplate("FootTemplate.ascx");BindDataGrid();}private void Button2_Click(object sender, System.EventArgs e){// Load templatesDataList1.AlternatingItemTemplate =Page.LoadTemplate("AltItemTempate2.ascx");DataList1.ItemTemplate = Page.LoadTemplate("ItemTemplate2.ascx");DataList1.HeaderTemplate = Page.LoadTemplate("HeadTemplate2.ascx");DataList1.FooterTemplate = Page.LoadTemplate("FootTemplate2.ascx");BindDataGrid();}? |
轉(zhuǎn)載于:https://www.cnblogs.com/netweb/archive/2008/12/09/1351201.html
總結(jié)
以上是生活随笔為你收集整理的ASP.NET中实现模版的动态加载的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 页面的未来
- 下一篇: Grove——.NET中的ORM实现