實現動態建立DataList模版

2021-04-08 20:14:05 字數 4719 閱讀 3257

現動態建立datalist模版

選擇自 duguguiyu1984 的 blog

關鍵字出處

為這個頭痛了有一段時間了。嘗試過一些方法也問了一些人檢視了一些文件。終於功夫不負有心人,得出了一種比較簡便清楚的方法。

首先要實現

itemplate

public

class

basetemplate

implements

itemplate

protected

templatetype

aslistitemtype

'模版的種類

protected

pnlmain

aspanel

'建立主面板

subnew

(byval

type

aslistitemtype)

me.templatetype = type

endsub

subinstantiatein(

byval

container

ascontrol)

implements

itemplate.instantiatein

me.pnlmain =

newpanel

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

endselect

container.controls.add(

me.pnlmain)

endsub

'資料item

protected

overridable

subitemtbinding(

byval

sender

asobject

, byval

e as

system.eventargs)

endsub

'頭item

protected

overridable

subheadertbinding(

byval

sender

asobject

, byval

e as

system.eventargs)

endsub

'交替item

protected

overridable

subalternatingitemtbinding(

byval

sender

asobject

, byval

e as

system.eventargs)

endsub

'腳item

protected

overridable

subfootertbinding(

byval

sender

asobject

, byval

e as

system.eventargs)

endsub

endclass

不是很晦澀。通過判斷型別,為panel載入繫結事件。下面在給出乙個實現的子類**:

public

class

mytalktemplate

inherits

basetemplate

subnew

(byval

type

aslistitemtype)

mybase

.new(type)

endsub

protected

overrides

subitemtbinding(

byval

sender

asobject

, byval

e as

system.eventargs)

dimpnl

aspanel

dimcontainer

asdatalistitem

pnl =

ctype

(sender, panel)

container =

ctype

(pnl.namingcontainer, datalistitem)

dimhtbl

asnew

htmltable

htbl.width = "100%"

'保證足夠寬

dimhrow

ashtmltablerow

dimhcell

ashtmltablecell

'

dimimg

asnew

webcontrols.image

img.height =

newunit(80)

'設定長和寬

img.width =

newunit(80)

img.imageurl =

string

.format("", container.dataitem("talkimg"))

hrow =

newhtmltablerow

hcell =

newhtmltablecell

hcell.rowspan = 2

hcell.controls.add(img)

hrow.cells.add(hcell)

'主題

hcell =

newhtmltablecell

hcell.align = "center"

hcell.controls.add(

newliteralcontrol("主題"))

hrow.cells.add(hcell)

htbl.rows.add(hrow)

hrow =

newhtmltablerow

hcell =

newhtmltablecell

hcell.width = "100%"

'保證足夠的寬度

hcell.valign = "top"

'豎直方向位於頂端

hcell.controls.add(

newliteralcontrol(

string

.format("", container.dataitem("talktitle"))))

hrow.cells.add(hcell)

htbl.rows.add(hrow)

'內容

hrow =

newhtmltablerow

hcell =

newhtmltablecell

hcell.colspan = 2

hcell.height = "1"

hcell.bgcolor = "#808080"

hrow.cells.add(hcell)

htbl.rows.add(hrow)

hrow =

newhtmltablerow

hcell =

newhtmltablecell

hcell.colspan = 2

hcell.controls.add(

newliteralcontrol(

string

.format("", container.dataitem("talktext"))))

hrow.cells.add(hcell)

htbl.rows.add(hrow)

me.pnlmain.controls.add(htbl)

endsub

寫的有點多餘哈。就是利用

container.dataitem

來建立各個控制項。

之所以不直接在實現介面時間直接利用繫結的資訊來建立控制項,是因為事件順序。

instantiatein

這個函式在基類的繫結事件之前執行,所以只能先新增乙個panel(或其他容器控制項),然後在基類繫結後,再呼叫panel的繫結事件。這樣就可以實現。

在此之前我曾經放棄使用datalist而自己寫table**來實現資料的顯示。但在交替顯示,分不同列顯示,制定樣式方面都很麻煩,所以還是推薦使用datalist動態載入模版來按行顯示資料。

不知道還有沒有更好的實現辦法,期待。。。

datalist實現分頁

原檔案test.aspx test.aspx.cs檔案 using system using system.data using system.configuration using system.collections using system.web using system.web.secur...

DataList實現摺疊效果

先看圖 這個功能可以用後台管理中 區域的繫結,商品大類,商品小類,商品資訊的管理 實現原理如下 先建設乙個資料表如 高校欄目表 截圖如下 parentid儲存父id的節點,childid儲存子id的節點,如果某一行中的parentid不為空,子節點為空,說明此行為父節點 而子節點的parentid對...

DataList實現主細表

資料庫 create table dbo tb empid userid bigint not null userloginname char 10 collate chinese prc ci as null on primary create table dbo tb employee user...