實(shí)現(xiàn)動(dòng)態(tài)建立DataList模版
2024-07-21 02:22:56
供稿:網(wǎng)友
為這個(gè)頭痛了有一段時(shí)間了。嘗試過(guò)一些方法也問(wèn)了一些人查看了一些文檔。終于功夫不負(fù)有心人,得出了一種比較簡(jiǎn)便清楚的方法。首先要實(shí)現(xiàn)itemplate接口,建立了一個(gè)基類相關(guān)代碼如下: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 '數(shù)據(jù)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不是很晦澀。通過(guò)判斷類型,為panel加載綁定事件。下面在給出一個(gè)實(shí)現(xiàn)的子類代碼: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%" '保證足夠?qū)?dim hrow as htmltablerow dim hcell as htmltablecell '圖片 dim img as new webcontrols.image img.height = new unit(80) '設(shè)定長(zhǎng)和寬 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) '內(nèi)容 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寫的有點(diǎn)多余哈。就是利用container.dataitem來(lái)建立各個(gè)控件。之所以不直接在實(shí)現(xiàn)接口時(shí)間直接利用綁定的信息來(lái)建立控件,是因?yàn)槭录樞颉nstantiatein這個(gè)函數(shù)在基類的綁定事件之前執(zhí)行,所以只能先添加一個(gè)panel(或其他容器控件),然后在基類綁定后,再調(diào)用panel的綁定事件。這樣就可以實(shí)現(xiàn)。在此之前我曾經(jīng)放棄使用datalist而自己寫table代碼來(lái)實(shí)現(xiàn)數(shù)據(jù)的顯示。但在交替顯示,分不同列顯示,制定樣式方面都很麻煩,所以還是推薦使用datalist動(dòng)態(tài)加載模版來(lái)按行顯示數(shù)據(jù)。不知道還有沒(méi)有更好的實(shí)現(xiàn)辦法,期待。。。