Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > CustomControls and Dynamically Added UserControls

Reply
Thread Tools

CustomControls and Dynamically Added UserControls

 
 
a
Guest
Posts: n/a
 
      11-12-2003
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


 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      11-12-2003
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" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using CustomControls in UserControls Nathan Sokalski ASP .Net Building Controls 0 02-27-2008 09:03 PM
Using CustomControls in UserControls Nathan Sokalski ASP .Net 0 02-27-2008 09:03 PM
Problem with Events in UserControls dynamically added to AJAX UpdatePanel JacekDr ASP .Net 3 08-26-2007 12:53 AM
Maintaining the look of dynamically added usercontrols Nathan Sokalski ASP .Net 7 06-19-2007 05:04 AM
Events not firing on dynamically added UserControls James Gerdes ASP .Net Building Controls 1 09-25-2003 03:41 AM



Advertisments