首页 > 开发 > .Net > 正文

ASP.NET中实现模版的动态加载

2020-02-03 16:37:57
字体:
来源:转载
供稿:网友
asp.net中,经常会使用到templates(模版)功能,比如在datagrid,datalist,repeater等控件中,使用templates,将会大大增强其功能。以往,我们一般是在设计程序时,就已经设置好控件中的模版是怎样的了。但是,有的时候,可能我们需要动态加载模版,比如,当你要求你的应用程序的界面风格随着用户的需求而变化时,你就需要到动态加载模版的功能了。但要注意的是,并不是所有的web控件都支持模版功能,而且要注意,哪些控件支持模版的哪些功能,下面简单列出了一些支持模版功能的控件:

  repeater控件,支持的模版有:

headertemplate, footertemplate, itemtemplate, alternatingitemtemplate, seperatortemplate.

  datelist控件,支持的模版有:

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

  datagrid控件,支持的模版有:

headertemplate, footertemplate, itemtemplate, edititemtemplate, pager.

  下面,我将以动态加载datalist控件的模版来说明如何动态加载模版:

  首先来了解动态加载模版的原理。在.net中,有templatecontrol类,这个类是page和usercontrol类的基类。它也同时定义了page和usercontrol类的基本功能。该类提供了两个方法:loadcontrol和loadtemplate。loadcontrol方法装载来自外部文件的控件,并且返回usercontrol类对象。而loadtemplate方法加载来自外部文件的模版并且返回的是itemplate对象。

  loadtemplate方法中,只有一个参数,参数值是外部模版文件的路径,并且返回itemplate对象。而datalist控件提供了一系列的属性,可以设置各种模版的属性,包括有alternatingitemtemplate, edititemtemplate, footertemplate, headertemplate, itemtemplate, selecteditemtemplate, 和 seperatortemplate,在下文中,将会看到相关介绍。



  接着,我们开始介绍例子,在示例程序中,是使用动态创建数据表和数据列的,并且将数据的创建封装到一个db类中,好让读者进一步回顾如何动态创建数据表,数据列等,并没用从数据库中提取(当然,你也可以用传统的读取数据库的方法),

public class db
{
 public db()
 { }
 /// <summary>
 /// method returns a dataset object filled with data
 /// </summary>
 public static dataset getdataset()
 {
  //创建dataset和datatable
  dataset ds = new dataset();
  datatable table = new datatable("records");
  datacolumn col;
  //增加一个列
  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 dataset
  ds.tables.add(table);
  // return dataset
  return ds;
 }
}
  接下来,我们首先创建若干个模版文件。我们先创建两组模版文件,每一组模版文件分别包含有header,footer,item,alternating item四个模版文件,保存成.ascx文件,这样,我们就有两类型风格的模版了,每类型风格的模版中都有自己的header,footer,item,alternating item子模版。下面为其中一个item模版文件,其他的类似。

<%@ 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>
  最后,我们开始创建应用程序,新建一个工程,添加两个按钮和一个datalist控件如下图


  之后创建一个binddatagrid的方法,将dataset绑定到datalist控件中去,代码如下:

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方法去加载我们已经写好的两套模版组中的模版,代码如下。

private void button1_click(object sender, system.eventargs e)
{
 // load templates
 datalist1.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 templates
 datalist1.alternatingitemtemplate =page.loadtemplate("altitemtempate2.ascx");
 datalist1.itemtemplate = page.loadtemplate("itemtemplate2.ascx");
 datalist1.headertemplate = page.loadtemplate("headtemplate2.ascx");
 datalist1.footertemplate = page.loadtemplate("foottemplate2.ascx");
 binddatagrid();
}
  运行效果如下两图,当点不同的按钮时,动态装载不同的模版风格。
 

菜鸟学堂:
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表