CustomControls and Dynamically Added UserControls

A

a

Hey gang.

THis is an ongoing issue that I have been having, so I am going to start a
new thread.

I have a (.VB File) CustomControl [called TEKTable] that is essentially a
table (inherits WebControl), with a definable number of columns. You pass
it an ArrayList of (.ASCX File) UserControls, and it lays them out for you.
I don't really know what SUB in TEKTable to put the code to build the table
in. At one point, it only worked if I set a property of TEKTable that had a
call to EnsureChildControls in it.

I want buttons on the .ASCX Controls to be handled by the ASCX control
itself, not bubble it up or anything. As it stands, the button seems to
work, but I get an error about "Multiple controls with the same ID
'Cookie29' were found. FindControl requires that controls have unique IDs."

Here is what TEKTable looks like. Where should I put the code, and what
would I need to do to handle ViewState. Also, do I need to load this
control only If Not Page.IsPostback?

Thasnk!

Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls

<DefaultProperty("Text"), ToolboxData("<{0}:TekTable
runat=server></{0}:TekTable>")> Public Class TekTable

Inherits System.Web.UI.WebControls.WebControl

Dim _text As String
Dim intMaxCols As Integer
Dim intMaxWidth As Integer
Dim alUC As New ArrayList

<Bindable(True), Category("Appearance"), DefaultValue("")> Property
[Text]() As String
Get
'EnsureChildControls()
Return _text
End Get

Set(ByVal Value As String)
'EnsureChildControls()
_text = Value
End Set
End Property
<Bindable(True), Category("Appearance"), DefaultValue("3")> Property
myMaxCols() As Integer
Get
'EnsureChildControls()
Return intMaxCols
End Get
Set(ByVal Value As Integer)
intMaxCols = Value
End Set
End Property
<Bindable(True), Category("Appearance"), DefaultValue("800")> Property
myMaxWidth() As Integer
Get
'EnsureChildControls()
Return intMaxWidth
End Get
Set(ByVal Value As Integer)
'EnsureChildControls()
intMaxWidth = Value
End Set
End Property
<Bindable(True), Category("Appearance"), DefaultValue("")> Property
myUC() As ArrayList
Get
'EnsureChildControls()
Return alUC
End Get
Set(ByVal Value As ArrayList)
'EnsureChildControls()
alUC = Value
End Set
End Property

Protected Overrides Sub Render(ByVal output As
System.Web.UI.HtmlTextWriter)
EnsureChildControls()
RenderChildren(output)
End Sub

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)

If Me.myUC.Count <> 0 Then

Dim tbl As New Table

Dim r As TableRow
Dim c As TableCell
Dim int As Integer
Dim booAdded As Boolean = False
Dim i As Integer = 1

For int = 0 To myUC.Count - 1
booAdded = False
If i = 1 Then
r = New TableRow
End If

c = New TableCell
c.VerticalAlign = VerticalAlign.Top
c.Controls.Add(myUC.Item(int))
r.Cells.Add(c)
If i = intMaxCols Then
tbl.Rows.Add(r)
booAdded = True
i = 0
End If
i += 1
tbl.Rows.Add(r)
Next

If booAdded = False Then
tbl.Rows.Add(r)
End If

Me.Controls.Add(tbl)

End If
End Sub

End Class
 
T

Teemu Keiski

Hi,

first steps to do:

Put the control to implement INamingContainer interface (needed as it is
composite custom control) and put the code to create child controls into
overridden CreateChildControls method without any IsPostBack check (they are
dynamical controls and need to be created on every request).

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist


a said:
Hey gang.

THis is an ongoing issue that I have been having, so I am going to start a
new thread.

I have a (.VB File) CustomControl [called TEKTable] that is essentially a
table (inherits WebControl), with a definable number of columns. You pass
it an ArrayList of (.ASCX File) UserControls, and it lays them out for you.
I don't really know what SUB in TEKTable to put the code to build the table
in. At one point, it only worked if I set a property of TEKTable that had a
call to EnsureChildControls in it.

I want buttons on the .ASCX Controls to be handled by the ASCX control
itself, not bubble it up or anything. As it stands, the button seems to
work, but I get an error about "Multiple controls with the same ID
'Cookie29' were found. FindControl requires that controls have unique IDs."

Here is what TEKTable looks like. Where should I put the code, and what
would I need to do to handle ViewState. Also, do I need to load this
control only If Not Page.IsPostback?

Thasnk!

Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls

<DefaultProperty("Text"), ToolboxData("<{0}:TekTable
runat=server></{0}:TekTable>")> Public Class TekTable

Inherits System.Web.UI.WebControls.WebControl

Dim _text As String
Dim intMaxCols As Integer
Dim intMaxWidth As Integer
Dim alUC As New ArrayList

<Bindable(True), Category("Appearance"), DefaultValue("")> Property
[Text]() As String
Get
'EnsureChildControls()
Return _text
End Get

Set(ByVal Value As String)
'EnsureChildControls()
_text = Value
End Set
End Property
<Bindable(True), Category("Appearance"), DefaultValue("3")> Property
myMaxCols() As Integer
Get
'EnsureChildControls()
Return intMaxCols
End Get
Set(ByVal Value As Integer)
intMaxCols = Value
End Set
End Property
<Bindable(True), Category("Appearance"), DefaultValue("800")> Property
myMaxWidth() As Integer
Get
'EnsureChildControls()
Return intMaxWidth
End Get
Set(ByVal Value As Integer)
'EnsureChildControls()
intMaxWidth = Value
End Set
End Property
<Bindable(True), Category("Appearance"), DefaultValue("")> Property
myUC() As ArrayList
Get
'EnsureChildControls()
Return alUC
End Get
Set(ByVal Value As ArrayList)
'EnsureChildControls()
alUC = Value
End Set
End Property

Protected Overrides Sub Render(ByVal output As
System.Web.UI.HtmlTextWriter)
EnsureChildControls()
RenderChildren(output)
End Sub

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)

If Me.myUC.Count <> 0 Then

Dim tbl As New Table

Dim r As TableRow
Dim c As TableCell
Dim int As Integer
Dim booAdded As Boolean = False
Dim i As Integer = 1

For int = 0 To myUC.Count - 1
booAdded = False
If i = 1 Then
r = New TableRow
End If

c = New TableCell
c.VerticalAlign = VerticalAlign.Top
c.Controls.Add(myUC.Item(int))
r.Cells.Add(c)
If i = intMaxCols Then
tbl.Rows.Add(r)
booAdded = True
i = 0
End If
i += 1
tbl.Rows.Add(r)
Next

If booAdded = False Then
tbl.Rows.Add(r)
End If

Me.Controls.Add(tbl)

End If
End Sub

End Class
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top