CustomControls and Dynamically Added UserControls

Discussion in 'ASP .Net Web Controls' started by a, Nov 12, 2003.

  1. a

    a Guest

    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
    a, Nov 12, 2003
    #1
    1. Advertising

  2. a

    Teemu Keiski Guest

    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" <> wrote in message news:...
    > 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
    >
    >
    Teemu Keiski, Nov 12, 2003
    #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. Nathan Sokalski
    Replies:
    7
    Views:
    359
    Cor Ligthert [MVP]
    Jun 19, 2007
  2. JacekDr
    Replies:
    3
    Views:
    1,086
    Muhammad Naveed Yaseen
    Aug 26, 2007
  3. Nathan Sokalski

    Using CustomControls in UserControls

    Nathan Sokalski, Feb 27, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    319
    Nathan Sokalski
    Feb 27, 2008
  4. James Gerdes

    Events not firing on dynamically added UserControls

    James Gerdes, Sep 23, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    270
    Victor Garcia Aprea [MVP]
    Sep 25, 2003
  5. Nathan Sokalski

    Using CustomControls in UserControls

    Nathan Sokalski, Feb 27, 2008, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    698
    Nathan Sokalski
    Feb 27, 2008
Loading...

Share This Page