[VB.NET]动态生成的repeater,为什么无法显示?

前端之家收集整理的这篇文章主要介绍了[VB.NET]动态生成的repeater,为什么无法显示?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

动态生成的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( "









" & _ " " & _ " ")) Case ListItemType.Item ph.Controls.Add(New LiteralControl( " ")) AddHandler ph.DataBinding,New EventHandler(AddressOf Item_DataBinding) Case ListItemType.AlternatingItem ph.Controls.Add(New LiteralControl( " ")) AddHandler ph.DataBinding,New EventHandler(AddressOf Item_DataBinding) Case ListItemType.Footer 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( "
")) End Select container.Controls.Add(ph) End Sub End Class Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click Dim DS As DataSet Dim sqlstr As String Dim MyConnection As sqlConnection Dim MyCommand As sqlDataAdapter Dim n As Integer = 1 MyConnection = New sqlConnection( "server=(local);database=Lib;UID=sa;Pwd=123 ") DS = New DataSet() Dim Repeater1 As New Repeater() Repeater1.HeaderTemplate = New MyTemplate(ListItemType.Header) Repeater1.ItemTemplate = New MyTemplate(ListItemType.Item) Repeater1.AlternatingItemTemplate = New MyTemplate(ListItemType.AlternatingItem) Repeater1.FooterTemplate = New MyTemplate(ListItemType.Footer) MyCommand = New sqlDataAdapter( "select * from abc ",MyConnection) MyCommand.Fill(DS,"abc ") Repeater1.DataSource = DS.Tables( "abc ") Repeater1.DataBind() ''Repeater2.DataSource = DS.Tables( "abc ") ''Repeater2.DataBind() Button1.Text = "finished " End Sub __________________________________________________________________________

猜你在找的VB相关文章