Control.Controls bug? Control's child controls missing at the run time.

Discussion in 'ASP .Net Building Controls' started by sinelnikov.andrei@gmail.com, Dec 7, 2005.

  1. Guest

    Hello,

    ..NET 1.1/VB.NET:

    I have a custom web control

    Public Class DatePicker
    Inherits Control
    Implements INamingContainer

    In the CreateChildControls I adding some controls to it:

    Protected Overrides Sub CreateChildControls()
    placeJavascript()
    MyBase.Controls.Add(New LiteralControl("<table
    class='Calendar'><tr><td>"))
    Dim txtTextBox As New System.Web.UI.WebControls.TextBox

    With txtTextBox
    If Len(m_ControlCssClass) > 0 Then
    .CssClass = m_ControlCssClass
    End If
    If Not (m_text = "") Then
    .Text = m_text
    End If
    If Not (m_Css = "") Then
    .CssClass = m_Css
    End If
    .ID = "foo"
    .Width = Unit.Pixel(80)
    .MaxLength = Me.DateType.Length
    End With

    MyBase.Controls.Add(txtTextBox)
    MyBase.Controls.Add(New LiteralControl( _
    "<img border='0' class='ImgNoSpace' alt='Calendar' " & _
    "src='" & imgDirectory + m_calendar & "' " & _
    "onclick=""javascript:popUpCalendar(document.all." &
    Me.ClientID & "_foo,document.all." & _
    Me.ClientID & "_foo, '" & m_DateType & "');"">"))
    MyBase.Controls.Add(New LiteralControl("</td></tr></table>"))
    End Sub

    Now, then I trying to do For Each ctl As Control In myControl.Controls
    on my page:

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    ' Call base load before we do any work at this level
    MyBase.OnLoad(e)

    dpFrom.ValidatingControlClientID = btnSearch.ClientID
    dpThrough.ValidatingControlClientID = btnSearch.ClientID

    For Each ctl As Control In dpFrom.Controls
    If TypeOf ctl Is TextBox Then
    dpThrough.SecondDateControlClientID = ctl.ClientID
    End If
    Next

    For Each ctl As Control In dpThrough.Controls
    If TypeOf ctl Is TextBox Then
    dpFrom.SecondDateControlClientID = ctl.ClientID
    End If
    Next

    End Sub

    I am getting nothing...

    So, if I set the breakpoint on the For Each ctl As Control In
    dpFrom.Controls and QuickWatch (or just Watch) dpFrom.Controls - I will
    see Count = 0.

    Now - interesting part: If I QuickWatch (or just Watch) just dpFrom and
    will expand Controls property - I will see Count = 4.

    And after that - if I QuickWatch (or just Watch) dpFrom.Controls - I
    will see Count = 4.

    I beleive that that is a bug.

    So, how I can get my control's child controls on the page? Should I do
    something differently then I adding child controls in my web control?

    Help! :)
     
    , Dec 7, 2005
    #1
    1. Advertising

  2. Teemu Keiski Guest

    Replied on *.webcontrols

    ***

    Hi,

    CreateChildControls will not be called unless something in your control
    invokes either FindControl (normally on postbacking control this is
    initiated on postback) or your control passes to PreRender phase, where
    controls are created automatically. E.g something should signal creating
    them.

    You can get accessing the Controls collection to cause EnsureChildControls
    to be called if youy override Controls property

    Public Overrides Controls As ControlsCollection
    Get
    EnsureChildControls()
    Return MyBAse.Controls
    End Get
    End Property

    With this approach child controls will be created when you loop though the
    collection.

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke


    <> wrote in message
    news:...
    > Hello,
    >
    > .NET 1.1/VB.NET:
    >
    > I have a custom web control
    >
    > Public Class DatePicker
    > Inherits Control
    > Implements INamingContainer
    >
    > In the CreateChildControls I adding some controls to it:
    >
    > Protected Overrides Sub CreateChildControls()
    > placeJavascript()
    > MyBase.Controls.Add(New LiteralControl("<table
    > class='Calendar'><tr><td>"))
    > Dim txtTextBox As New System.Web.UI.WebControls.TextBox
    >
    > With txtTextBox
    > If Len(m_ControlCssClass) > 0 Then
    > .CssClass = m_ControlCssClass
    > End If
    > If Not (m_text = "") Then
    > .Text = m_text
    > End If
    > If Not (m_Css = "") Then
    > .CssClass = m_Css
    > End If
    > .ID = "foo"
    > .Width = Unit.Pixel(80)
    > .MaxLength = Me.DateType.Length
    > End With
    >
    > MyBase.Controls.Add(txtTextBox)
    > MyBase.Controls.Add(New LiteralControl( _
    > "<img border='0' class='ImgNoSpace' alt='Calendar' " & _
    > "src='" & imgDirectory + m_calendar & "' " & _
    > "onclick=""javascript:popUpCalendar(document.all." &
    > Me.ClientID & "_foo,document.all." & _
    > Me.ClientID & "_foo, '" & m_DateType & "');"">"))
    > MyBase.Controls.Add(New LiteralControl("</td></tr></table>"))
    > End Sub
    >
    > Now, then I trying to do For Each ctl As Control In myControl.Controls
    > on my page:
    >
    > Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    > ' Call base load before we do any work at this level
    > MyBase.OnLoad(e)
    >
    > dpFrom.ValidatingControlClientID = btnSearch.ClientID
    > dpThrough.ValidatingControlClientID = btnSearch.ClientID
    >
    > For Each ctl As Control In dpFrom.Controls
    > If TypeOf ctl Is TextBox Then
    > dpThrough.SecondDateControlClientID = ctl.ClientID
    > End If
    > Next
    >
    > For Each ctl As Control In dpThrough.Controls
    > If TypeOf ctl Is TextBox Then
    > dpFrom.SecondDateControlClientID = ctl.ClientID
    > End If
    > Next
    >
    > End Sub
    >
    > I am getting nothing...
    >
    > So, if I set the breakpoint on the For Each ctl As Control In
    > dpFrom.Controls and QuickWatch (or just Watch) dpFrom.Controls - I will
    > see Count = 0.
    >
    > Now - interesting part: If I QuickWatch (or just Watch) just dpFrom and
    > will expand Controls property - I will see Count = 4.
    >
    > And after that - if I QuickWatch (or just Watch) dpFrom.Controls - I
    > will see Count = 4.
    >
    > I beleive that that is a bug.
    >
    > So, how I can get my control's child controls on the page? Should I do
    > something differently then I adding child controls in my web control?
    >
    > Help! :)
    >
     
    Teemu Keiski, Dec 8, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Rodriguez
    Replies:
    23
    Views:
    1,134
    David Schwartz
    Dec 9, 2003
  2. Pierre Yves
    Replies:
    2
    Views:
    491
    Pierre Yves
    Jan 10, 2008
  3. Zarko Gajic

    Child control renders at desing-time as if it is in run-time...

    Zarko Gajic, Mar 14, 2005, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    136
    Zarko Gajic
    Mar 16, 2005
  4. Replies:
    0
    Views:
    209
  5. Replies:
    1
    Views:
    192
    Teemu Keiski
    Dec 8, 2005
Loading...

Share This Page