:o( .... Can't make a simple composite control work

Discussion in 'ASP .Net Web Controls' started by M O J O, Aug 5, 2005.

  1. M O J O

    M O J O Guest

    Hi,

    I'm playing around with creating composite controls.

    I want to create a composite web control, that will create 10 textboxes
    and be able to return all their text values.

    Here's my code so far...

    --------------------------------------------------------------------------

    Imports System
    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Text
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Collections.Specialized


    <DefaultProperty("Type"), ToolboxData("<{0}:MyTextBox
    runat=server></{0}:MyTextBox>")> _
    Public Class MyTextBox
    Inherits CompositeControl

    Dim arr As Generic.List(Of TextBox)


    Protected Overrides Sub RenderContents(ByVal output As HtmlTextWriter)
    If Not (Site Is Nothing) Then
    ' In design mode...
    output.Write("TextBox")
    Else
    ' In runtime...
    Call EnsureChildControls()
    Call RenderChildren(output)
    End If
    End Sub


    Protected Overrides Sub CreateChildControls()
    Dim arr As New Generic.List(Of TextBox)
    For i As Integer = 0 To 10
    Dim t As New TextBox
    arr.Add(t)
    Me.Controls.Add(t)
    Next
    End Sub


    Public Function GetAllText() As String
    Me.EnsureChildControls()

    Dim sb As New System.Text.StringBuilder

    For Each t As TextBox In arr ' <- Error!!
    sb.Append(t.Text & vbCrLf)
    Next

    Return sb.ToString
    End Function

    End Class

    --------------------------------------------------------------------------

    But I can't make it work.

    In the GetAllText function, the arr variable is nothing eventhough I
    called the EnsureChildContols method.

    What am I doing wrong, and what is the best way (practice) of creating
    such a composite control? Can I do it better differently?

    Thanks!!

    M O J O
     
    M O J O, Aug 5, 2005
    #1
    1. Advertising

  2. M O J O

    Teemu Keiski Guest

    Hi,

    you basically instantiate a local generic array in CreateChildControls
    (local to the method) and therefore the global one, declared in class body,
    actually never gets assigned anything.

    Change the code in CreateChildControls to:

    Protected Overrides Sub CreateChildControls()
    'NOTE THIS LINE
    arr = New Generic.List(Of TextBox)

    For i As Integer = 0 To 10
    Dim t As New TextBox
    arr.Add(t)
    Me.Controls.Add(t)
    Next
    End Sub


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





    "M O J O" <mojo@_no_spam_delete_this_newwebsolutions.dk> wrote in message
    news:...
    > Hi,
    >
    > I'm playing around with creating composite controls.
    >
    > I want to create a composite web control, that will create 10 textboxes
    > and be able to return all their text values.
    >
    > Here's my code so far...
    >
    > --------------------------------------------------------------------------
    >
    > Imports System
    > Imports System.Collections.Generic
    > Imports System.ComponentModel
    > Imports System.Text
    > Imports System.Web
    > Imports System.Web.UI
    > Imports System.Web.UI.WebControls
    > Imports System.Collections.Specialized
    >
    >
    > <DefaultProperty("Type"), ToolboxData("<{0}:MyTextBox
    > runat=server></{0}:MyTextBox>")> _
    > Public Class MyTextBox
    > Inherits CompositeControl
    >
    > Dim arr As Generic.List(Of TextBox)
    >
    >
    > Protected Overrides Sub RenderContents(ByVal output As HtmlTextWriter)
    > If Not (Site Is Nothing) Then
    > ' In design mode...
    > output.Write("TextBox")
    > Else
    > ' In runtime...
    > Call EnsureChildControls()
    > Call RenderChildren(output)
    > End If
    > End Sub
    >
    >
    > Protected Overrides Sub CreateChildControls()
    > Dim arr As New Generic.List(Of TextBox)
    > For i As Integer = 0 To 10
    > Dim t As New TextBox
    > arr.Add(t)
    > Me.Controls.Add(t)
    > Next
    > End Sub
    >
    >
    > Public Function GetAllText() As String
    > Me.EnsureChildControls()
    >
    > Dim sb As New System.Text.StringBuilder
    >
    > For Each t As TextBox In arr ' <- Error!!
    > sb.Append(t.Text & vbCrLf)
    > Next
    >
    > Return sb.ToString
    > End Function
    >
    > End Class
    >
    > --------------------------------------------------------------------------
    >
    > But I can't make it work.
    >
    > In the GetAllText function, the arr variable is nothing eventhough I
    > called the EnsureChildContols method.
    >
    > What am I doing wrong, and what is the best way (practice) of creating
    > such a composite control? Can I do it better differently?
    >
    > Thanks!!
    >
    > M O J O
     
    Teemu Keiski, Aug 7, 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. Replies:
    0
    Views:
    740
  2. sleigh
    Replies:
    1
    Views:
    2,725
    sleigh
    Feb 12, 2004
  3. Chad
    Replies:
    0
    Views:
    243
  4. Mike

    Composite vs non composite Controls

    Mike, Mar 10, 2005, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    284
    Sundararajan
    Mar 11, 2005
  5. Xeno Campanoli
    Replies:
    1
    Views:
    360
    James Britt
    Jul 1, 2005
Loading...

Share This Page