实现动态建立DataList模版
2024-07-21 02:22:56
供稿:网友
为这个头痛了有一段时间了。尝试过一些方法也问了一些人查看了一些文档。终于功夫不负有心人,得出了一种比较简便清楚的方法。首先要实现itemplate接口,建立了一个基类相关代码如下:public class basetemplate implements itemplate protected templatetype as listitemtype '模版的种类 protected pnlmain as panel '建立主面板 sub new(byval type as listitemtype) me.templatetype = type end sub sub instantiatein(byval container as control) implements itemplate.instantiatein me.pnlmain = new panel select case templatetype case listitemtype.header addhandler me.pnlmain.databinding, addressof me.headertbinding case listitemtype.item addhandler me.pnlmain.databinding, addressof me.itemtbinding case listitemtype.alternatingitem addhandler me.pnlmain.databinding, addressof me.alternatingitemtbinding case listitemtype.footer addhandler me.pnlmain.databinding, addressof me.footertbinding end select container.controls.add(me.pnlmain) end sub '数据item protected overridable sub itemtbinding(byval sender as object, byval e as system.eventargs) end sub '头item protected overridable sub headertbinding(byval sender as object, byval e as system.eventargs) end sub '交替item protected overridable sub alternatingitemtbinding(byval sender as object, byval e as system.eventargs) end sub '脚item protected overridable sub footertbinding(byval sender as object, byval e as system.eventargs) end sub end class不是很晦涩。通过判断类型,为panel加载绑定事件。下面在给出一个实现的子类代码:public class mytalktemplate inherits basetemplate sub new(byval type as listitemtype) mybase.new(type) end sub protected overrides sub itemtbinding(byval sender as object, byval e as system.eventargs) dim pnl as panel dim container as datalistitem pnl = ctype(sender, panel) container = ctype(pnl.namingcontainer, datalistitem) dim htbl as new htmltable htbl.width = "100%" '保证足够宽 dim hrow as htmltablerow dim hcell as htmltablecell '图片 dim img as new webcontrols.image img.height = new unit(80) '设定长和宽 img.width = new unit(80) img.imageurl = string.format("{0}", container.dataitem("talkimg")) hrow = new htmltablerow hcell = new htmltablecell hcell.rowspan = 2 hcell.controls.add(img) hrow.cells.add(hcell) '主题 hcell = new htmltablecell hcell.align = "center" hcell.controls.add(new literalcontrol("<big>主题</big>")) hrow.cells.add(hcell) htbl.rows.add(hrow) hrow = new htmltablerow hcell = new htmltablecell hcell.width = "100%" '保证足够的宽度 hcell.valign = "top" '竖直方向位于顶端 hcell.controls.add(new literalcontrol(string.format("<h5>{0}</h5>", container.dataitem("talktitle")))) hrow.cells.add(hcell) htbl.rows.add(hrow) '内容 hrow = new htmltablerow hcell = new htmltablecell hcell.colspan = 2 hcell.height = "1" hcell.bgcolor = "#808080" hrow.cells.add(hcell) htbl.rows.add(hrow) hrow = new htmltablerow hcell = new htmltablecell hcell.colspan = 2 hcell.controls.add(new literalcontrol(string.format("{0}", container.dataitem("talktext")))) hrow.cells.add(hcell) htbl.rows.add(hrow) me.pnlmain.controls.add(htbl) end sub写的有点多余哈。就是利用container.dataitem来建立各个控件。之所以不直接在实现接口时间直接利用绑定的信息来建立控件,是因为事件顺序。instantiatein这个函数在基类的绑定事件之前执行,所以只能先添加一个panel(或其他容器控件),然后在基类绑定后,再调用panel的绑定事件。这样就可以实现。在此之前我曾经放弃使用datalist而自己写table代码来实现数据的显示。但在交替显示,分不同列显示,制定样式方面都很麻烦,所以还是推荐使用datalist动态加载模版来按行显示数据。不知道还有没有更好的实现办法,期待。。。