自定义Repeater模板(原创)
曾碰到一情况,一个页面上要显示四种类型的数据,当时时间紧就采用了比较笨的方法,采用了四个Repeater来实现,切换起来总是感觉不爽.偶抽空就查了下资料,改编了MSDN的一些例子.实现了动态生成Repeater模板的功能.
一.自定义模板代码
View Code
1 public class RepeaterTemplate:ITemplate
2 {
3 private ListItemType templateType;
4 private IList<String> headerNames;
5 private IList<String> bindColumns;
6 private IList<int> percentageOfWidth;
7
8 public RepeaterTemplate(ListItemType type,IList<String> headernames,IList<String> bindcolumns,IList<int> percentageofwidth)
9 {
10 templateType = type;
11 headerNames = headernames;
12 bindColumns = bindcolumns;
13 percentageOfWidth = percentageofwidth;
14 }
15
16 public void InstantiateIn(Control container)
17 {
18 int percentageOfWidthIndex = 0;
19 PlaceHolder ph = new PlaceHolder();
20 Label[] lbls = new Label[headerNames.Count];
21 for (int index = 0; index < lbls.Length; index++)
22 {
23 lbls[index] = new Label();
24 lbls[index].ID = "lbl" + index.ToString();
25 }
26
27 switch (templateType)
28 {
29 case ListItemType.Header:
30 ph.Controls.Add(new LiteralControl("<table border=\"0\" style=\"width: 100%;\" class=\"TableStyle\" cellpadding=\"0\">"));
31 ph.Controls.Add(new LiteralControl("<tr id=\"TableTitle\" align=\"center\" valign=\"middle\">"));
32 ph.Controls.Add(new LiteralControl("<td width=\"2%\" style=\"border-left: 0px solid #000;\"> </td>"));
33 percentageOfWidthIndex = 0;
34 foreach (String headername in headerNames)
35 {
36 ph.Controls.Add(new LiteralControl("<td width=\""+percentageOfWidth[percentageOfWidthIndex]+"%\">"+headername+"</td>"));
37 }
38 ph.Controls.Add(new LiteralControl("</tr>"));
39 break;
40 case ListItemType.Item:
41 ph.Controls.Add(new LiteralControl("<tr align=\"center\" align=\"center\" valign=\"middle\" class=\"TableDetail2\"> "));
42 ph.Controls.Add(new LiteralControl("<td width=\"1%\" style=\"border-left: 0px solid #000;\"> </td>"));
43 foreach (Label lbl in lbls)
44 {
45 ph.Controls.Add(new LiteralControl("<td>"));
46 ph.Controls.Add(lbl);
47 ph.Controls.Add(new LiteralControl("</td>"));
48 }
49 ph.DataBinding += new EventHandler(Item_DataBinding);
50 ph.Controls.Add(new LiteralControl("</tr>"));
51 break;
52 case ListItemType.Footer:
53 ph.Controls.Add(new LiteralControl("</table>"));
54 break;
55 }
56 container.Controls.Add(ph);
57 }
58 void Item_DataBinding(object sender, System.EventArgs e)
59 {
60 PlaceHolder ph = (PlaceHolder)sender;
61 RepeaterItem ri = (RepeaterItem)ph.NamingContainer;
62 for (int index = 0; index < headerNames.Count; index++)
63 {
64 ((Label)ph.FindControl("lbl" + index.ToString())).Text =DataBinder.Eval(ri.DataItem,bindColumns[index]).ToString();
65 }
66 }
67
68 }
二.调用代码
View Code
1 List<int> t=new List<int>();
2 t.Add(10);
3 t.Add(10);
4 t.Add(11);
5 t.Add(10);
6 t.Add(10);
7 List<string> name=new List<string>();
8 name.Add("入库批次号");
9 name.Add("资产名称");
10 name.Add("规格型号");
11 name.Add("数量");
12 name.Add("计量单位");
13 List<string> colum=new List<string>();
14 colum.Add("gid");
15 colum.Add("name");
16 colum.Add("name");
17 colum.Add("name");
18 colum.Add("name");
19 Repeater1.HeaderTemplate = new RepeaterTemplate(ListItemType.Header, name, colum, t);
20 Repeater1.ItemTemplate = new RepeaterTemplate(ListItemType.Item, name, colum, t);
21 Repeater1.FooterTemplate = new RepeaterTemplate(ListItemType.Footer, name, colum, t);
22 Repeater1.DataSource = g_assetinstoragemanager.AssetInstorageManager.GetAssetInformationByTypeAndEntryBy(AssetType.FIXEDASSET,"admin");
23 Repeater1.DataBind();