M
M O J O
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
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