實現動態建立DataList模版

來源:互聯網
上載者:User
動態 為這個頭痛了有一段時間了。嘗試過一些方法也問了一些人查看了一些文檔。終於功夫不負有心人,得出了一種比較簡便清楚的方法。首先要實現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動態載入模版來按行顯示資料。不知道還有沒有更好的實現辦法,期待。。。



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。