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

首頁 > 編程 > .NET > 正文

ASP.NET中實現(xiàn)模版的動態(tài)加載

2024-07-10 13:13:02
字體:
供稿:網(wǎng)友

asp.net中,經(jīng)常會使用到templates(模版)功能,比如在datagrid,datalist,repeater等控件中,使用templates,將會大大增強其功能。以往,我們一般是在設(shè)計程序時,就已經(jīng)設(shè)置好控件中的模版是怎樣的了。但是,有的時候,可能我們需要動態(tài)加載模版,比如,當(dāng)你要求你的應(yīng)用程序的界面風(fēng)格隨著用戶的需求而變化時,你就需要到動態(tài)加載模版的功能了。但要注意的是,并不是所有的web控件都支持模版功能,而且要注意,哪些控件支持模版的哪些功能,下面簡單列出了一些支持模版功能的控件:

repeater控件,支持的模版有:

headertemplate,%20footertemplate,%20itemtemplate,%20alternatingitemtemplate,%20seperatortemplate.

datelist控件,支持的模版有:

headertemplate,%20footertemplate,%20itemtemplate,%20alternatingitemtemplate,%20separatortemplate,%20selecteditemtemplate,%20edititemtemplate.

datagrid控件,支持的模版有:

headertemplate,%20footertemplate,%20itemtemplate,%20edititemtemplate,%20pager.

下面,我將以動態(tài)加載datalist控件的模版來說明如何動態(tài)加載模版:

首先來了解動態(tài)加載模版的原理。在.net中,有templatecontrol類,這個類是page和usercontrol類的基類。它也同時定義了page和usercontrol類的基本功能。該類提供了兩個方法:loadcontrol和loadtemplate。loadcontrol方法裝載來自外部文件的控件,并且返回usercontrol類對象。而loadtemplate方法加載來自外部文件的模版并且返回的是itemplate對象。

loadtemplate方法中,只有一個參數(shù),參數(shù)值是外部模版文件的路徑,并且返回itemplate對象。而datalist控件提供了一系列的屬性,可以設(shè)置各種模版的屬性,包括有alternatingitemtemplate,%20edititemtemplate,%20footertemplate,%20headertemplate,%20itemtemplate,%20selecteditemtemplate,%20和%20seperatortemplate,在下文中,將會看到相關(guān)介紹。

接著,我們開始介紹例子,在示例程序中,是使用動態(tài)創(chuàng)建數(shù)據(jù)表和數(shù)據(jù)列的,并且將數(shù)據(jù)的創(chuàng)建封裝到一個db類中,好讓讀者進(jìn)一步回顧如何動態(tài)創(chuàng)建數(shù)據(jù)表,數(shù)據(jù)列等,并沒用從數(shù)據(jù)庫中提取(當(dāng)然,你也可以用傳統(tǒng)的讀取數(shù)據(jù)庫的方法),

以下為引用的內(nèi)容:
public%20class%20db{public%20db(){%20}///%20<summary>///%20method%20returns%20a%20dataset%20object%20filled%20with%20data///%20</summary>public%20static%20dataset%20getdataset(){//創(chuàng)建dataset和datatabledataset%20ds%20=%20new%20dataset();datatable%20table%20=%20new%20datatable("records");datacolumn%20col;//增加一個列col%20=%20new%20datacolumn();col.datatype%20=%20system.type.gettype("system.int32");col.columnname%20=%20"id";col.readonly%20=%20true;col.unique%20=%20true;table.columns.add(col);col%20=%20new%20datacolumn();col.datatype%20=%20system.type.gettype("system.string");col.columnname%20=%20"name";col.autoincrement%20=%20false;col.caption%20=%20"name";col.readonly%20=%20false;col.unique%20=%20false;table.columns.add(col);col%20=%20new%20datacolumn();col.datatype%20=%20system.type.gettype("system.string");col.columnname%20=%20"address";col.autoincrement%20=%20false;col.caption%20=%20"address";col.readonly%20=%20false;col.unique%20=%20false;table.columns.add(col);//增加一條記錄datarow%20row%20=%20table.newrow();row["id"]%20=%201001;row["name"]%20=%20"melanie%20giard";row["address"]%20=%20"23rd%20street,%20park%20road,%20ny%20city,%20ny";table.rows.add(row);row%20=%20table.newrow();row["id"]%20=%201002;row["name"]%20=%20"puneet%20nehra";row["address"]%20=%20"3rd%20blvd,%20ashok%20vihar,%20new%20delhi";table.rows.add(row);row%20=%20table.newrow();row["id"]%20=%201003;row["name"]%20=%20"raj%20mehta";row["address"]%20=%20"nagrath%20chowk,%20jabalpur";table.rows.add(row);row%20=%20table.newrow();row["id"]%20=%201004;row["name"]%20=%20"max%20muller";row["address"]%20=%20"25%20north%20street,%20hernigton,%20russia";table.rows.add(row);//%20add%20datatable%20to%20datasetds.tables.add(table);//%20return%20datasetreturn%20ds;}}%20

接下來,我們首先創(chuàng)建若干個模版文件。我們先創(chuàng)建兩組模版文件,每一組模版文件分別包含有header,footer,item,alternating%20item四個模版文件,保存成.ascx文件,這樣,我們就有兩類型風(fēng)格的模版了,每類型風(fēng)格的模版中都有自己的header,footer,item,alternating%20item子模版。下面為其中一個item模版文件,其他的類似。

以下為引用的內(nèi)容:
<%@%20control%20language="vb"%20%><font%20face="verdana"%20color="green"%20size="2"><b>id:%20</b><%#%20databinder.eval(ctype(container,%20datalistitem).dataitem,%20"id")%20%><b>name:%20</b><%#%20databinder.eval(ctype(container,%20datalistitem).dataitem,%20"name")%20%><br><b>address:%20</b><%#%20databinder.eval(ctype(container,%20datalistitem).dataitem,%20"address")%20%><p></font>%20

最后,我們開始創(chuàng)建應(yīng)用程序,新建一個工程,添加兩個按鈕和一個datalist控件如下圖:

之后創(chuàng)建一個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();}} 

最后,分別為兩個按鈕的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();} 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 黑水县| 合作市| 婺源县| 昌邑市| 淳化县| 杭锦后旗| 南陵县| 垫江县| 拜泉县| 浦东新区| 利津县| 瑞昌市| 伊金霍洛旗| 读书| 兰溪市| 正蓝旗| 远安县| 庆城县| 元江| 尼勒克县| 布拖县| 抚顺市| 城市| 永德县| 栾城县| 奇台县| 汾阳市| 玉田县| 柘城县| 新野县| 陇西县| 九龙坡区| 龙海市| 石棉县| 云浮市| 论坛| 彰化县| 安新县| 灯塔市| 彰化县| 仪陇县|