日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

建設(shè)網(wǎng)站之ASP.NET中實現(xiàn)模版的動態(tài)加載

 liuqg 2006-03-17
ASP.NET中實現(xiàn)模版的動態(tài)加載
上傳日期:2005-4-28


  ASP.NET中,經(jīng)常會使用到templ ates(模版)功能,比如在datagrid,datalis t,repeater等控件中,使用templates,將會大 大增強(qiáng)其功能。以往,我們一般是在設(shè)計程序時,就已經(jīng)設(shè)置好控件 中的模版是怎樣的了。但是,有的時候,可能我們需要動態(tài)加載模版 ,比如,當(dāng)你要求你的應(yīng)用程序的界面風(fēng)格隨著用戶的需求而變化時 ,你就需要到動態(tài)加載模版的功能了。但要注意的是,并不是所有的 web控件都支持模版功能,而且要注意,哪些控件支持模版的哪些 功能,下面簡單列出了一些支持模版功能的控件:
  Re peater控件,支持的模版有:
HeaderTemp late, FooterTemplate, ItemTemp late, AlternatingItemTemplate, SeperatorTemplate.
  Datel ist控件,支持的模版有:
HeaderTemplat e, FooterTemplate, ItemTemplat e, AlternatingItemTemplate, Se paratorTemplate, SelectedItemT emplate, EditItemTemplate.
  Datagrid控件,支持的模版有:
Header Template, FooterTemplate, Item Template, EditItemTemplate, Pa ger.
  下面,我將以動態(tài)加載datalist控件 的模版來說明如何動態(tài)加載模版:
  首先來了解動態(tài)加載 模版的原理。在.NET中,有templatecontrol類 ,這個類是page和usercontrol類的基類。它也同時 定義了page和usercontrol類的基本功能。該類提供 了兩個方法:loadcontrol和loadtemplate 。Loadcontrol方法裝載來自外部文件的控件,并且返回 usercontrol類對象。而loadtemplate方法 加載來自外部文件的模版并且返回的是Itemplate對象。< br>  Loadtemplate方法中,只有一個參數(shù),參數(shù) 值是外部模版文件的路徑,并且返回itemplate對象。而d atalist控件提供了一系列的屬性,可以設(shè)置各種模版的屬性 ,包括有AlternatingItemTemplate, E ditItemTemplate, FooterTemplat e, HeaderTemplate, ItemTemplat e, SelectedItemTemplate, 和 Sep eratorTemplate,在下文中,將會看到相關(guān)介紹。< br>  接著,我們開始介紹例子,在示例程序中,是使用動態(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ù)庫的方法),
pu blic class DB{ public DB() { }  /// <
summary>
 /// Metho d returns a DataSet object fil led with data /// <
/summary >
 public static DataSet Get DataSet() {  //創(chuàng)建dataset和datat able   DataSet ds = new DataSe t();
  DataTable table = new Da taTable("Records");
  DataColum n col;
  //增加一個列   col = new D ataColumn();
  col.DataType = S ystem.Type.GetType("System.Int 32");
  col.ColumnName = "ID";
   col.ReadOnly = true;
  col.Uni que = true;
  table.Columns.Add (col);

  col = new DataColu mn();
  col.DataType = System.T ype.GetType("System.String");
   col.ColumnName = "Name";
  col .AutoIncrement = false;
  col.C aption = "Name";
  col.ReadOnly = false;
  col.Unique = false;
  table.Columns.Add(col);
  col = new DataColumn();
  col.Data Type = System.Type.GetType("Sy stem.String");
  col.ColumnName = "Address";
  col.AutoIncreme nt = false;
  col.Caption = "Ad dress";
  col.ReadOnly = false;
  col.Unique = false;
  table.C olumns.Add(col);

  //增加一條記錄   DataRow row = table.NewRow( );
  row["ID"] = 1001;
  row["Na me"] = "Melanie Giard";
  row[" Address"] = "23rd Street, Park Road, NY City, NY";
  table.Ro ws.Add(row);
  row = table.NewR ow();
  row["ID"] = 1002;
  row[ "Name"] = "Puneet Nehra";
  row ["Address"] = "3rd Blvd, Ashok Vihar, New Delhi";
  table.Row s.Add(row);
  row = table.NewRo w();
  row["ID"] = 1003;
  row[" Name"] = "Raj Mehta";
  row["Ad dress"] = "Nagrath Chowk, Jaba lpur";
  table.Rows.Add(row);
   row = table.NewRow();
  row["ID "] = 1004;
  row["Name"] = "Max Muller";
  row["Address"] = "2 5 North Street, Hernigton, Rus sia";
  table.Rows.Add(row);

  // Add DataTable to DataSet   ds.Tables.Add(table);
  // Re turn DataSet  return ds;
 }} < br>  接下來,我們首先創(chuàng)建若干個模版文件。我們先創(chuàng)建兩組 模版文件,每一組模版文件分別包含有header,footer ,item,alternating item四個模版文件,保 存成.ascx文件,這樣,我們就有兩類型風(fēng)格的模版了,每類型 風(fēng)格的模版中都有自己的header,footer,item, alternating item子模版。下面為其中一個ite m模版文件,其他的類似。
<
%@ Control Language="VB" %>
<
FONT f ace="verdana" color="green" si ze="2">
<
b>
ID: <
/b& gt;
<
%# DataBinder.Eval(CTyp e(Container, DataListItem).Dat aItem, "ID") %>
<
b>
Nam e: <
/b>
<
%# DataBinder .Eval(CType(Container, DataLis tItem).DataItem, "Name") %>
<
br>
<
b>
Address: &l t;
/b>
<
%# DataBinder.Eval (CType(Container, DataListItem ).DataItem, "Address") %>
&l t;
p>
<
/FONT>

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


  之后創(chuàng)建一 個binddatagrid的方法,將dataset綁定到da talist控件中去,代碼如下:
private vo id BindDataGrid(){  dtSet = DB .GetDataSet();
 DataList1.Data Source = dtSet.Tables[0].Defau ltView;
 DataList1.DataBind();
} private void Page_Load(objec t sender, System.EventArgs e){  if(!IsPostBack)  {   BindDat aGrid();
 }}
  最后,分別為兩個按鈕 的clcik事件添加代碼,分別使用page.loadtemp late方法去加載我們已經(jīng)寫好的兩套模版組中的模版,代碼如下 。
private void Button1_Clic k(object sender, System.EventA rgs e){ // Load templates Data List1.AlternatingItemTemplate =  Page.LoadTemplate("AltItemT empate.ascx");
 DataList1.ItemT emplate =Page.LoadTemplate("It emTemplate.ascx");
 DataList1.H eaderTemplate =Page.LoadTempla te("HeadTemplate.ascx");
 DataL ist1.FooterTemplate = Page.Loa dTemplate("FootTemplate.ascx") ;
 BindDataGrid();
} private voi d Button2_Click(object sender, System.EventArgs e){ // Load templates DataList1.Alternatin gItemTemplate =Page.LoadTempla te("AltItemTempate2.ascx");
 Da taList1.ItemTemplate = Page.Lo adTemplate("ItemTemplate2.ascx ");
 DataList1.HeaderTemplate = Page.LoadTemplate("HeadTempla te2.ascx");
 DataList1.FooterTe mplate = Page.LoadTemplate("Fo otTemplate2.ascx");
 BindDataGr id();
}
  運行效果如下兩圖,當(dāng)點不同的按 鈕時,動態(tài)裝載不同的模版風(fēng)格。


< br>

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約