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

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

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

    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.a ll." &
    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! :-)

    sinelnikov.andrei@gmail.com Guest

  2. Similar Questions and Discussions

    1. Get Child Controls at design-time
      I have a Webcontrol that was supposed to act like a panel control. I manipulated a TemplatedControlDesigner to make an "Edit mode". I save my...
    2. Building child controls on Design Time
      Hi , I am trying to create a custom control similar to datagrid.Grid contain some thing like data column where i am able to add column during...
    3. user controls: dynamiclly added child controls dont survive post back ?
      hi, i have some strange behaviour: i've created a web user control that add's some child controls (e.g: textbox, image buttons) to its control...
    4. Custom Control with Child Controls
      Hi, I have created 2 custom control : MyControl and C1 I have following tags in my aspx file : <myCtrl:MyControl runat=server> <myCtrl:C1...
    5. Accessing Properties of Custom Controls child Controls
      I am using a Custom Control on a page which renders a button control if required. I need to access the child button control's properties (i.e....
  3. #2

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

    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
    [url]http://blogs.aspadvice.com/joteke[/url]


    <sinelnikov.andrei@gmail.com> wrote in message
    news:1133991965.027536.149070@o13g2000cwo.googlegr oups.com...
    > 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.a ll." &
    > 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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139