动态生成的repeater,为什么无法显示?
说明:
1)将Shared Sub Item_DataBinding(ByVal sender As Object,ByVal e As System.EventArgs) ... ... end sub 放在
Public Class MyTemplate... ...End class外面时,无法编译通过,提示是“语句在命名空间中无效”,所以放在了class定义之中。
2)代码中的repeater2是通过designed直接添加的,可以显示,说明可以正常查询到数据。
3)比较奇怪,在web form中手动添加一个repeater控件,id是repeater1,然后就可以了,可这笔算是动态创建啊?
Public Class MyTemplate
Implements System.Web.UI.ITemplate
Dim templateType As ListItemType
Sub New(ByVal type As ListItemType)
templateType = type
End Sub
Shared Sub Item_DataBinding(ByVal sender As Object,ByVal e As System.EventArgs)
Dim ph As PlaceHolder = CType(sender,PlaceHolder)
Dim ri As RepeaterItem = CType(ph.NamingContainer,RepeaterItem)
Dim item1Value As Double = _
Convert.ToDouble(DataBinder.Eval(ri.DataItem,"name "))
Dim item2Value As Double = _
Convert.ToDouble(DataBinder.Eval(ri.DataItem,"hobby "))
CType(ph.FindControl( "item1 "),Label).Text = item1Value.ToString()
CType(ph.FindControl( "item2 "),Label).Text = item2Value
End Sub
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) _
Implements System.Web.UI.ITemplate.InstantiateIn
Dim ph As New PlaceHolder()
Dim item1 As New Label()
Dim item2 As New Label()
item1.ID = "item1 "
item2.ID = "item2 "
Select Case (templateType)
Case ListItemType.Header
ph.Controls.Add(New LiteralControl( "
Category ID | " & _ "Category Name |
")) ph.Controls.Add(item1) ph.Controls.Add(New LiteralControl( " |
")) ph.Controls.Add(item2) ph.Controls.Add(New LiteralControl( " |
")) ph.Controls.Add(item1) ph.Controls.Add(New LiteralControl( " |
")) ph.Controls.Add(item2) ph.Controls.Add(New LiteralControl( " |