ASP.NET中,经常会使用到templates(模版)功能,比如在datagrid,datalist,repeater等控件中,使用templates,将会大大增强其功能。以往,我们一般是在
设计程序时,就已经设置好控件中的模版是怎样的了。但是,有的时候,可能我们需要动态加载模版,比如,当你要求你的应用程序的界面风格随着用户的需求而变化时,你就需要到动态加载模版的功能了。但要注意的是,并不是所有的
web控件都支持模版功能,而且要注意,哪些控件支持模版的哪些功能,下面简单列出了一些支持模版功能的控件:
)g|)Yb$e.]l0Repeater控件,支持的模版有:
ITPUB个人空间w8SNa3gHeaderTemplate, FooterTemplate, ItemTemplate, AlternatingItemTemplate, SeperatorTemplate.
Lz+LJ-yw2i3kx0Datelist控件,支持的模版有:
6wE%b5jz|)a0HeaderTemplate, FooterTemplate, ItemTemplate, AlternatingItemTemplate, SeparatorTemplate, SelectedItemTemplate, EditItemTemplate.
Y?d/At}5T[m0Datagrid控件,支持的模版有:
K Zd*M-| i#sa'ZH0HeaderTemplate, FooterTemplate, ItemTemplate, EditItemTemplate, Pager.
ITPUB个人空间
`/dS6[6s下面,我将以动态加载datalist控件的模版来说明如何动态加载模版:
ITPUB个人空间dFrt0qz首先来了解动态加载模版的原理。在.NET中,有templatecontrol类,这个类是page和usercontrol类的基类。它也同时定义了page和usercontrol类的基本功能。该类提供了两个方法:loadcontrol和loadtemplate。Loadcontrol方法装载来自外部文件的控件,并且返回usercontrol类对象。而loadtemplate方法加载来自外部文件的模版并且返回的是Itemplate对象。
ITPUB个人空间p
Tw.@Rw*lLoadtemplate方法中,只有一个参数,参数值是外部模版文件的路径,并且返回itemplate对象。而datalist控件提供了一系列的属性,可以设置各种模版的属性,包括有AlternatingItemTemplate, EditItemTemplate, FooterTemplate, HeaderTemplate, ItemTemplate, SelectedItemTemplate, 和 SeperatorTemplate,在下文中,将会看到相关介绍。
ITPUB个人空间2xJK tmDj接着,我们开始介绍例子,在示例程序中,是使用动态创建数据表和数据列的,并且将数据的创建封装到一个Db类中,好让读者进一步回顾如何动态创建数据表,数据列等,并没用从
数据库中提取(当然,你也可以用传统的读取数据库的方法),public class DB{
ITPUB个人空间z/{o$xLw9Dpublic DB()
0][}K*d\/[1cpJ0{ }
ITPUB个人空间QO1P}+UG#^*USP/// <summary>
,h(],[#n.F#e[Z$T
]'z3q0/// Method returns a DataSet object filled with
data1fB;z J6jH-q0/// </summary>
]-sCBVf0public static DataSet GetDataSet()
~6\m-\#`u,UuyiO0{
ITPUB个人空间wIpWI@//创建dataset和datatable
k(I1{Zx#ez!K0DataSet ds = new DataSet();
ITPUB个人空间/R+C8\$R&orYj,O)nDataTable table = new DataTable("Records");
ITPUB个人空间
rKhQj*YDataColumn col;
ITPUB个人空间@h O0p.ZE-~5fg//增加一个列
;~8Qo+Nw;u:U3E3~R0col = new DataColumn();
ITPUB个人空间4MG {T;Ncol.DataType = System.Type.GetType("System.Int32");
[3zj]-?&B3x0col.ColumnName = "ID";
'ym7pMBC[;Dv?!t0col.ReadOnly = true;
ITPUB个人空间3BJ3bn,M6xV)fI_f_col.Unique = true;
o aE2k/Q` O#rQ0table.Columns.Add(col);
:I
tB/FS\qs[0col = new DataColumn();
UD1TiCty0col.DataType = System.Type.GetType("System.String");
ITPUB个人空间$mi r
?+U;P;Eufcol.ColumnName = "Name";
:O.E/Z,oD`7|
j0col.AutoIncrement = false;
.slE+WR
W#a
R0col.Caption = "Name";
1lB:gjf:Pb!Ikr0col.ReadOnly = false;
9wI5R+XD0col.Unique = false;
8d8P8P0U}4ca2k"^0table.Columns.Add(col);
:Yu
\2g!KB
v9o0col = new DataColumn();
ITPUB个人空间)q6o9F7FWGs&Tcol.DataType = System.Type.GetType("System.String");
B:_
wd|q8Xq0col.ColumnName = "Address";
ITPUB个人空间4o0J
{oy^4Z/@col.AutoIncrement = false;
&\9ee!\3m,z6T0col.Caption = "Address";
&j%U"s
pw_8E!pb0col.ReadOnly = false;
+IMSIh3o_ KS0col.Unique = false;
ITPUB个人空间!]v.I.iP0ER;`table.Columns.Add(col);
ITPUB个人空间 H"St6pDQA4p0JS//增加一条记录
!E`+h%P$mr#|0DataRow row = table.NewRow();
5GV#d3Rx0row["ID"] = 1001;
ITPUB个人空间o8[9f]Ykjrow["Name"] = "Melanie Giard";
.~"g,Deb'^0row["Address"] = "23rd Street, Park Road, NY City, NY";
ITPUB个人空间%VO)dfS8@8Utable.Rows.Add(row);
ITPUB个人空间&t"NUBPV5prow = table.NewRow();
;z3\-FaAmigOr0row["ID"] = 1002;
ITPUB个人空间*N
} s8PPe%_row["Name"] = "Puneet Nehra";
`1G$e0?!W l.@0row["Address"] = "3rd Blvd, Ashok Vihar, New Delhi";
ITPUB个人空间&g4K
}-\ey$FFtable.Rows.Add(row);
ITPUB个人空间D;k*Q R(krhrow = table.NewRow();
ITPUB个人空间~
[2a g_d2lvC:orow["ID"] = 1003;
-~)P!R
b,Q5KSYY P3K9r0row["Name"] = "Raj Mehta";
ITPUB个人空间.jy
g,Z U;hrow["Address"] = "Nagrath Chowk, Jabalpur";
ITPUB个人空间l|l-j6e;Aotable.Rows.Add(row);
5J)Wi0~l(Q0row = table.NewRow();
I&aNBHf[L0row["ID"] = 1004;
d u
ntY.F^'jik0row["Name"] = "Max Muller";
uu5ixIy0row["Address"] = "25 North Street, Hernigton, Russia";
DDc;y(opL!tJ0table.Rows.Add(row);
)U5TAB?-w |7z0// Add DataTable to DataSet
{vO-I:PwnA3m^}0ds.Tables.Add(table);
ITPUB个人空间
] g r$T2fC n
o L5|// Return DataSet
ITPUB个人空间1[ EvzT2d Ireturn ds;
/yN/`1Ua|{0}}
ITPUB个人空间 [ NkD TW"O接下来,我们首先创建若干个模版文件。我们先创建两组模版文件,每一组模版文件分别包含有header,footer,item,alternating item四个模版文件,保存成.ascx文件,这样,我们就有两类型风格的模版了,每类型风格的模版中都有自己的header,footer,item,alternating item子模版。下面为其中一个item模版文件,其他的类似。
E&\X?frg0<%@ Control Language="VB" %>
ITPUB个人空间Ad0M(\N<FONT face="verdana" color="green" size="2"><b>ID: </b>
ITPUB个人空间[0z$cSg"LN<%# DataBinder.Eval(CType(Container, DataListItem).DataItem, "ID") %>
[/o*[g2F$Ysd0<b>Name: </b>
ITPUB个人空间I3S|/X5Mc<%# DataBinder.Eval(CType(Container, DataListItem).DataItem, "Name") %>
ITPUB个人空间f3|8u(W;K5V*Zn#j<br>
ITPUB个人空间+o.KFWz/qo<b>Address: </b>
T R}ZU/G$?-d0<%# DataBinder.Eval(CType(Container, DataListItem).DataItem, "Address") %>
6t8Vb `
e!I0<p>
R%?/b&`|
H+y3UB7j0</FONT>
D
o,Dah}d1z0最后,我们开始创建应用程序,新建一个工程,添加两个按钮和一个datalist控件如下图:
Ys~'hH9PMW B#L0 |
| 图1 |
ITPUB个人空间j+PZuYI-e%@0g"y之后创建一个binddatagrid的方法,将dataset绑定到datalist控件中去,代码如下:
`-R2y7r
~ O{j nA0private void BindDataGrid(){
|#X/O!vj'g5V?0dtSet = DB.GetDataSet();
ITPUB个人空间i*j2o!`%S;UL
J5WDataList1.DataSource = dtSet.Tables[0].DefaultView;
{Q:D
z|0DataList1.DataBind();}private void Page_Load(object sender, System.EventArgs e){
ITPUB个人空间b hK&}W:T nif(!IsPostBack)
ITPUB个人空间&M_G FpnG{
ITPUB个人空间-Cz!}NG0odu
[BindDataGrid();
ITPUB个人空间5dI]%B~4HJ[8l}}
ITPUB个人空间
LAP6w|*Z最后,分别为两个按钮的clcik事件添加代码,分别使用page.loadtemplate方法去加载我们已经写好的两套模版组中的模版,代码如下:
y3S}#z"Z0private void Button1_Click(object sender, System.EventArgs e){
ITPUB个人空间[L,bDl]4j*u#D&jZ// Load templates
ITPUB个人空间Zo/LM3_DataList1.AlternatingItemTemplate =
IVl*?Wg0Page.LoadTemplate("AltItemTempate.ascx");
ITPUB个人空间K'Rfaw+iDataList1.ItemTemplate =Page.LoadTemplate("ItemTemplate.ascx");
ITPUB个人空间JFvR8@2O;y"tDataList1.HeaderTemplate =Page.LoadTemplate("HeadTemplate.ascx");
ITPUB个人空间0y3xP3k*`4e)^DataList1.FooterTemplate = Page.LoadTemplate("FootTemplate.ascx");
ITPUB个人空间 Di%?$s%gFPBindDataGrid();}private void Button2_Click(object sender, System.EventArgs e){
QR[
J"k0// Load templates
ITPUB个人空间^7a/a'O fl;sMh/VDataList1.AlternatingItemTemplate =Page.LoadTemplate("AltItemTempate2.ascx");
ITPUB个人空间 d[m6_-Dj;rfdeDataList1.ItemTemplate = Page.LoadTemplate("ItemTemplate2.ascx");
e c)jz ol,w V0X0DataList1.HeaderTemplate = Page.LoadTemplate("HeadTemplate2.ascx");
*yrI3R:Pd
A+H
H0DataList1.FooterTemplate = Page.LoadTemplate("FootTemplate2.ascx");
)Gs'jH\5]}-e(Z0BindDataGrid();}