0

我正在设计一个用户控件,它试图根据下面的示例标记在页面上创建一个带有各种 TextBox 或 DropDownList 元素的过滤器栏:

<gf:GridFilterBar runat="server">
    <filters>
        <filter Label="Field1" Type="TextBox" />
        <filter Label="Field2" Type="DropDownList" />
    </filters>
</gf:GridFilterBar>

使用另一篇文章的灵感,我创建了正确解析此标记并读取每个预期子控件的属性的代码。我遇到的问题是何时在屏幕上实际呈现此信息。我从“Filter”类的“New”子中初始化的每个控件都不会出现在屏幕上。当我在“新”子中放置一个断点并跟踪正在发生的事情时,我可以看到 Filter.New 子被遍历两次并且值被读入,但我从该子中初始化的其他任何内容都没有对页面产生任何影响尽管如此,据我所知,这一切都已成功创建。以下是仅读取 Label 属性的代码示例:

Imports System
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls


Public Class GridFilterBar
Inherits System.Web.UI.UserControl

Private _Filters As New FiltersClass(Me)

<PersistenceMode(PersistenceMode.InnerProperty)> _
Public ReadOnly Property Filters() As FiltersClass
    Get
        Return _Filters
    End Get
End Property

Private Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
    DDL.Visible = True
End Sub
End Class

Public Class FiltersClass
Inherits ControlCollection

Public Sub New(ByVal owner As Control)
    MyBase.New(owner)
End Sub

Public Overrides Sub Add(ByVal child As System.Web.UI.Control)
    MyBase.Add(New Filter(child))
End Sub

End Class

Public Class Filter
Inherits HtmlGenericControl

Public Sub New(ByVal GenericControl As HtmlGenericControl)
    Label = GenericControl.Attributes("Label")
    Dim lit As New Literal
    lit.Text = Label.ToString
    Me.Controls.Add(lit)
End Sub

Public Property Label As String = String.Empty

Public Overrides Function ToString() As String
    Return Me.Label
End Function

End Class

谁能发现我做错了什么?

4

1 回答 1

-1

我能够回答我的问题。我在主类中为 CreateChildControls 添加了一个覆盖子,并使用 For Each 循环从每个新初始化的“过滤器”中获取属性集

Protected Overrides Sub CreateChildControls()

    For Each filter In Filters

        Dim lit As New Literal
        lit.Text = filter.Label
        Controls.Add(lit)

    Next filter

End Sub

这将 Filter.New 降级为简单地获取属性:

Public Sub New(ByVal GenericControl As HtmlGenericControl)
    Label = GenericControl.Attributes("Label")
End Sub
于 2012-10-15T14:00:44.500 回答