2012-10-15 47 views
0

我設計試圖根據下面的樣本標記來創建與頁面上的各種文本框或DropDownList的元件的過濾杆的用戶控制:如何在頁面上呈現我的用戶控件的內部屬性?

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

從另一個後使用靈感,我已經創建後面的代碼正確分析這個標記並讀入每個想要的子控件的屬性。我遇到的問題是實際在屏幕上呈現此信息的時間。我從「過濾器」類的「新建」子項中初始化的每個控件都不會出現在屏幕上。當我在「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 

任何人都可以發現我做錯了什麼嗎?

+0

請參閱下面的答案。 – Ryan

回答

-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 
相關問題