天天看點

自定義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  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;\">&nbsp;</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;\">&nbsp;</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();      

轉載于:https://www.cnblogs.com/liangjie/archive/2011/10/13/2210760.html