ASP.NET中实现模版的动态加载 |
|
上传日期:2005-4-28 |
ASP.NET中,经常会使用到templ ates(模版)功能,比如在datagrid,datalis t,repeater等控件中,使用templates,将会大 大增强其功能。以往,我们一般是在设计程序时,就已经设置好控件 中的模版是怎样的了。但是,有的时候,可能我们需要动态加载模版 ,比如,当你要求你的应用程序的界面风格随着用户的需求而变化时 ,你就需要到动态加载模版的功能了。但要注意的是,并不是所有的 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. 下面,我将以动态加载datalist控件 的模版来说明如何动态加载模版: 首先来了解动态加载 模版的原理。在.NET中,有templatecontrol类 ,这个类是page和usercontrol类的基类。它也同时 定义了page和usercontrol类的基本功能。该类提供 了两个方法:loadcontrol和loadtemplate 。Loadcontrol方法装载来自外部文件的控件,并且返回 usercontrol类对象。而loadtemplate方法 加载来自外部文件的模版并且返回的是Itemplate对象。< br> Loadtemplate方法中,只有一个参数,参数 值是外部模版文件的路径,并且返回itemplate对象。而d atalist控件提供了一系列的属性,可以设置各种模版的属性 ,包括有AlternatingItemTemplate, E ditItemTemplate, FooterTemplat e, HeaderTemplate, ItemTemplat e, SelectedItemTemplate, 和 Sep eratorTemplate,在下文中,将会看到相关介绍。< br> 接着,我们开始介绍例子,在示例程序中,是使用动态创 建数据表和数据列的,并且将数据的创建封装到一个Db类中,好让 读者进一步回顾如何动态创建数据表,数据列等,并没用从数据库中 提取(当然,你也可以用传统的读取数据库的方法), pu blic class DB{ public DB() { } /// < summary> /// Metho d returns a DataSet object fil led with data /// < /summary > public static DataSet Get DataSet() { //创建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> 接下来,我们首先创建若干个模版文件。我们先创建两组 模版文件,每一组模版文件分别包含有header,footer ,item,alternating item四个模版文件,保 存成.ascx文件,这样,我们就有两类型风格的模版了,每类型 风格的模版中都有自己的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>
最后, 我们开始创建应用程序,新建一个工程,添加两个按钮和一个dat alist控件如下图
之后创建一 个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方法去加载我们已经写好的两套模版组中的模版,代码如下 。 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(); } 运行效果如下两图,当点不同的按 钮时,动态装载不同的模版风格。
< br> |
|