动态生成Repeater
代码
1 public class MyTemplate : ITemplate
2 {
3 System.Web.UI.WebControls.ListItemType templateType;
4 static int titleLength;
5 public MyTemplate(System.Web.UI.WebControls.ListItemType type)
6 {
7 templateType = type;
8 }
9
10 public void InstantiateIn(System.Web.UI.Control container)
11 {
12 PlaceHolder ph = new PlaceHolder();
13 Label item1 = new Label();
14 HyperLink item2 = new HyperLink();
15 Label item3 = new Label();
16 item1.ID = "item1";
17 item2.ID = "item2";
18
19 switch (templateType)
20 {
21 case ListItemType.Header:
22 ph.Controls.Add(new LiteralControl("<table border=\"0\">"));
23 break;
24 case ListItemType.Item:
25 ph.Controls.Add(new LiteralControl("<tr><td>"));
26 ph.Controls.Add(item2);
27 ph.Controls.Add(new LiteralControl("</td><td>"));
28 ph.Controls.Add(item1);
29 ph.Controls.Add(new LiteralControl("</td></tr>"));
30 ph.DataBinding += new EventHandler(Item_DataBinding);
31 break;
32 case ListItemType.AlternatingItem:
33 ph.Controls.Add(new LiteralControl("<tr><td>"));
34 ph.Controls.Add(item2);
35 ph.Controls.Add(new LiteralControl("</td><td>"));
36 ph.Controls.Add(item1);
37 ph.Controls.Add(new LiteralControl("</td></tr>"));
38 ph.DataBinding += new EventHandler(Item_DataBinding);
39 break;
40 case ListItemType.Footer:
41 ph.Controls.Add(new LiteralControl("</table>"));
42 break;
43 }
44 container.Controls.Add(ph);
45 }
46
47 static void Item_DataBinding(object sender, System.EventArgs e)
48 {
49 PlaceHolder ph = (PlaceHolder)sender;
50 RepeaterItem ri = (RepeaterItem)ph.NamingContainer;
51 DateTime item1Value = (DateTime)DataBinder.Eval(ri.DataItem, "PublishDate");
52 String item2Value = (String)DataBinder.Eval(ri.DataItem, "Title");
53 ((Label)ph.FindControl("item1")).Text = item1Value.ToString("yyyy-MM-dd");
54 }
55 }
2 {
3 System.Web.UI.WebControls.ListItemType templateType;
4 static int titleLength;
5 public MyTemplate(System.Web.UI.WebControls.ListItemType type)
6 {
7 templateType = type;
8 }
9
10 public void InstantiateIn(System.Web.UI.Control container)
11 {
12 PlaceHolder ph = new PlaceHolder();
13 Label item1 = new Label();
14 HyperLink item2 = new HyperLink();
15 Label item3 = new Label();
16 item1.ID = "item1";
17 item2.ID = "item2";
18
19 switch (templateType)
20 {
21 case ListItemType.Header:
22 ph.Controls.Add(new LiteralControl("<table border=\"0\">"));
23 break;
24 case ListItemType.Item:
25 ph.Controls.Add(new LiteralControl("<tr><td>"));
26 ph.Controls.Add(item2);
27 ph.Controls.Add(new LiteralControl("</td><td>"));
28 ph.Controls.Add(item1);
29 ph.Controls.Add(new LiteralControl("</td></tr>"));
30 ph.DataBinding += new EventHandler(Item_DataBinding);
31 break;
32 case ListItemType.AlternatingItem:
33 ph.Controls.Add(new LiteralControl("<tr><td>"));
34 ph.Controls.Add(item2);
35 ph.Controls.Add(new LiteralControl("</td><td>"));
36 ph.Controls.Add(item1);
37 ph.Controls.Add(new LiteralControl("</td></tr>"));
38 ph.DataBinding += new EventHandler(Item_DataBinding);
39 break;
40 case ListItemType.Footer:
41 ph.Controls.Add(new LiteralControl("</table>"));
42 break;
43 }
44 container.Controls.Add(ph);
45 }
46
47 static void Item_DataBinding(object sender, System.EventArgs e)
48 {
49 PlaceHolder ph = (PlaceHolder)sender;
50 RepeaterItem ri = (RepeaterItem)ph.NamingContainer;
51 DateTime item1Value = (DateTime)DataBinder.Eval(ri.DataItem, "PublishDate");
52 String item2Value = (String)DataBinder.Eval(ri.DataItem, "Title");
53 ((Label)ph.FindControl("item1")).Text = item1Value.ToString("yyyy-MM-dd");
54 }
55 }
代码
1 Repeater noticeRepeater = new Repeater();
2 List<DemoClass> noticeList = new List<DemoClass>();
3
4
5 noticeRepeater.HeaderTemplate = new MyTemplate(ListItemType.Header);
6 noticeRepeater.ItemTemplate = new MyTemplate(ListItemType.Item);
7 noticeRepeater.AlternatingItemTemplate = new MyTemplate(ListItemType.AlternatingItem);
8 noticeRepeater.FooterTemplate = new MyTemplate(ListItemType.Footer);
2 List<DemoClass> noticeList = new List<DemoClass>();
3
4
5 noticeRepeater.HeaderTemplate = new MyTemplate(ListItemType.Header);
6 noticeRepeater.ItemTemplate = new MyTemplate(ListItemType.Item);
7 noticeRepeater.AlternatingItemTemplate = new MyTemplate(ListItemType.AlternatingItem);
8 noticeRepeater.FooterTemplate = new MyTemplate(ListItemType.Footer);