user controls: dynamiclly added child controls dont survive post back ?

Discussion in 'ASP .Net Building Controls' started by Wolfgang Brucker, Jan 14, 2004.

  1. hi,

    i have some strange behaviour: i've created a web user control that add's
    some child controls (e.g: textbox, image buttons) to its control collection
    (= Me.Controls) at runtime.

    no prob. but after a postback gets fired (and the web page reloads) the
    control collectionb (Me.Controls) is NOTHING. where are the controls
    that i've added?

    i even tryed to replace Me.Controls with my own collection, but the very
    same effect occurs (value of NOTHING after postback).

    what the hell i'm doing wrong (code incl.) and what do i have to do
    to make the added child controls surviving a post back ?

    thx,
    wolfgang brucker
    delphi software



    ############ Code comes here ############

    Imports System

    Imports System.ComponentModel

    Imports System.Text

    Imports System.Web

    Imports System.Web.UI

    Imports System.Web.UI.WebControls



    Namespace WebControls



    Public Enum en_TV_Resolution

    PAL_160x120

    PAL_320x240

    PAL_352x288

    PAL_352x576

    PAL_480x576

    PAL_720x576

    PAL_768x608

    End Enum



    Public Enum en_ctrlITVcontent_ChildControlType

    TextBox

    DynamicTextBox

    ImageButton

    End Enum



    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissi
    ons.SecurityAction.Demand, Name:="FullTrust")> _

    Public Class ctrlITVcontent

    Inherits WebControl

    Implements IPostBackEventHandler

    Implements INamingContainer

    Public Event Click As EventHandler



    Private myTVResolution As en_TV_Resolution

    Private myCOL As New Collection







    <Description("Gets or sets the original TV Resolution in Pixel."), _

    Category("Layout"), _

    DefaultValue(en_TV_Resolution.PAL_720x576)> _

    Public Property OriginalTVResolution() As en_TV_Resolution

    Get

    Return myTVResolution

    End Get

    Set(ByVal Value As en_TV_Resolution)

    myTVResolution = Value

    End Set

    End Property







    Public Sub AddNewTextBox(ByVal PosX As Integer, ByVal PosY As Integer, ByVal
    TVWidth As Integer, _

    Optional ByVal InitText As String = "")

    If TVWidth = 0 Then

    Exit Sub

    End If

    Dim ctrl As New System.Web.UI.WebControls.TextBox

    Dim css As String = "POSITION: absolute; "

    Dim x As Integer = TVpx2PCpx_Width(PosX)

    Dim y As Integer = TVpx2PCpx_Height(PosY)

    Dim w As Integer = TVSize2PCSize_Width(TVWidth) 'TVpx2PCpx_Width(TVWidth)

    css &= "LEFT: " & x.ToString & "px; "

    css &= "TOP: " & y.ToString & "px;"

    ctrl.Attributes.Add("style", css)

    ctrl.Attributes.Add("width", w)

    ctrl.Width = Unit.Pixel(w)

    ctrl.Text = InitText

    ctrl.ID = CreateID(en_ctrlITVcontent_ChildControlType.TextBox)

    Me.Controls.Add(ctrl)

    myCOL.Add(ctrl)

    End Sub



    Public Sub AddNewDynamicTextBox(ByVal PosX As Integer, ByVal PosY As
    Integer, ByVal TVSize_W As Integer, _

    ' ...

    ' like "AddNewTextBox"

    ' ....

    End Sub





    Public Sub AddNewImageButton(ByVal PosX As Integer, ByVal PosY As Integer,
    ByVal TVSize As Byte, _

    ByVal PictureURL As String)

    ' ...

    ' like "AddNewTextBox"

    ' ...

    End Sub



    Public Sub RemoveAllChildControls()

    If HasControls() Then

    Dim i As Integer

    For i = Controls.Count - 1 To 0 Step -1

    Controls(i).Dispose()

    Next

    End If

    End Sub



    Private Function PAL_Width() As Integer

    Select Case OriginalTVResolution

    Case en_TV_Resolution.PAL_160x120 : Return 160

    Case en_TV_Resolution.PAL_320x240 : Return 320

    Case en_TV_Resolution.PAL_352x288 : Return 352

    Case en_TV_Resolution.PAL_352x576 : Return 352

    Case en_TV_Resolution.PAL_480x576 : Return 480

    Case en_TV_Resolution.PAL_720x576 : Return 720

    Case en_TV_Resolution.PAL_768x608 : Return 768

    End Select

    End Function



    Private Function PAL_Height() As Integer

    Select Case OriginalTVResolution

    Case en_TV_Resolution.PAL_160x120 : Return 160

    Case en_TV_Resolution.PAL_320x240 : Return 240

    Case en_TV_Resolution.PAL_352x288 : Return 288

    Case en_TV_Resolution.PAL_352x576 : Return 576

    Case en_TV_Resolution.PAL_480x576 : Return 576

    Case en_TV_Resolution.PAL_720x576 : Return 576

    Case en_TV_Resolution.PAL_768x608 : Return 608

    End Select

    End Function



    ' size 1=1/1 2=1/2 4=1/4 8=1/8 des fernse-bildes (und sogleich in PC-Pixel
    ummappen)

    Private Function TVSize2PCSize_Width(ByVal TVSize As Integer) As Integer

    Return TVpx2PCpx_Width(PAL_Width() * (1 / TVSize))

    End Function

    Private Function TVSize2PCSize_Height(ByVal TVSize As Integer) As Integer

    Return TVpx2PCpx_Height(PAL_Height() * (1 / TVSize))

    End Function

    ' PosX [tv] = 240 => wird in diesem Ctrl auf PosX [pc] = 166 gemappt

    Private Function TVpx2PCpx_Width(ByVal TVPosX As Integer) As Integer

    Return CInt(TVPosX / (PAL_Width() / Val(Me.Width.ToString)))

    End Function

    Private Function TVpx2PCpx_Height(ByVal TVPosY As Integer) As Integer

    Return CInt(TVPosY / (PAL_Height() / Val(Me.Height.ToString)))

    End Function



    Private Function CreateID(ByVal ctrl_type As
    en_ctrlITVcontent_ChildControlType) As String

    Dim id_nr As Integer = Me.Controls.Count

    Dim kz As String

    Select Case ctrl_type

    Case en_ctrlITVcontent_ChildControlType.TextBox : kz = "txtPreV" &
    CStr(id_nr)

    Case en_ctrlITVcontent_ChildControlType.DynamicTextBox : kz =
    "txtCtrlPreView" & (id_nr)

    Case en_ctrlITVcontent_ChildControlType.ImageButton : kz = "imgCtrlPreView"
    & (id_nr)

    End Select

    Return kz

    End Function



    'Protected Overrides Sub AddAttributesToRender(ByVal writer As
    System.Web.UI.HtmlTextWriter)

    ''


    'End Sub



    ' invoke delegates registered with the click event.

    Protected Overridable Sub OnClick(ByVal e As EventArgs)

    RaiseEvent Click(Me, e)

    End Sub

    ' raise click event to owner of this control

    Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements
    System.Web.UI.IPostBackEventHandler.RaisePostBackEvent

    OnClick(New EventArgs)

    End Sub



    Protected Overrides Sub CreateChildControls()

    Me.InitializeComponent()

    End Sub



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

    MyBase.OnPreRender(e)

    End Sub

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

    Me.EnsureChildControls()

    MyBase.Render(writer)

    End Sub

    Private Sub InitializeComponent()

    If Me.Controls Is Nothing Then

    Dim ctrl As Control

    For Each ctrl In myCOL

    Me.Controls.Add(ctrl)

    Next

    End If

    End Sub





    Private Sub EventHandler_ImageClicked(ByVal sender As Object, ByVal e As
    System.Web.UI.ImageClickEventArgs)

    Dim x As String

    x = ""

    End Sub



    Private Sub EventHandler_DynamicTextChanged(ByVal sender As Object, ByVal e
    As System.EventArgs)

    Dim x As String

    x = ""

    End Sub



    Protected Overrides Sub RenderChildren(ByVal writer As
    System.Web.UI.HtmlTextWriter)

    If HasControls() Then

    Dim i As Integer

    Dim c As New Control

    For i = Controls.Count - 1 To 0 Step -1

    Controls(i).RenderControl(writer)

    Next

    End If

    End Sub

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

    '

    End Sub

    End Class

    End Namespace





    ############ CODE-END ############
     
    Wolfgang Brucker, Jan 14, 2004
    #1
    1. Advertising

  2. Hi Wolfgang,

    Are you always recreating the child controls (no matter IsPostBack value,
    etc) ? You need to recreate them every time for them to be available as
    there is no builtin support to "remember" which controls were previously
    created.
    Also, when you start dealing with dynamically created controls it may be
    better to start looking for a composite custom control instead of a Web User
    Control.

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx

    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    "Wolfgang Brucker" <> wrote in message
    news:...
    > hi,
    >
    > i have some strange behaviour: i've created a web user control that

    add's
    > some child controls (e.g: textbox, image buttons) to its control

    collection
    > (= Me.Controls) at runtime.
    >
    > no prob. but after a postback gets fired (and the web page reloads) the
    > control collectionb (Me.Controls) is NOTHING. where are the controls
    > that i've added?
    >
    > i even tryed to replace Me.Controls with my own collection, but the very
    > same effect occurs (value of NOTHING after postback).
    >
    > what the hell i'm doing wrong (code incl.) and what do i have to do
    > to make the added child controls surviving a post back ?
    >
    > thx,
    > wolfgang brucker
    > delphi software
    >
    >
    >
    > ############ Code comes here ############
    >
    > Imports System
    >
    > Imports System.ComponentModel
    >
    > Imports System.Text
    >
    > Imports System.Web
    >
    > Imports System.Web.UI
    >
    > Imports System.Web.UI.WebControls
    >
    >
    >
    > Namespace WebControls
    >
    >
    >
    > Public Enum en_TV_Resolution
    >
    > PAL_160x120
    >
    > PAL_320x240
    >
    > PAL_352x288
    >
    > PAL_352x576
    >
    > PAL_480x576
    >
    > PAL_720x576
    >
    > PAL_768x608
    >
    > End Enum
    >
    >
    >
    > Public Enum en_ctrlITVcontent_ChildControlType
    >
    > TextBox
    >
    > DynamicTextBox
    >
    > ImageButton
    >
    > End Enum
    >
    >
    >
    >

    <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissi
    > ons.SecurityAction.Demand, Name:="FullTrust")> _
    >
    > Public Class ctrlITVcontent
    >
    > Inherits WebControl
    >
    > Implements IPostBackEventHandler
    >
    > Implements INamingContainer
    >
    > Public Event Click As EventHandler
    >
    >
    >
    > Private myTVResolution As en_TV_Resolution
    >
    > Private myCOL As New Collection
    >
    >
    >
    >
    >
    >
    >
    > <Description("Gets or sets the original TV Resolution in Pixel."), _
    >
    > Category("Layout"), _
    >
    > DefaultValue(en_TV_Resolution.PAL_720x576)> _
    >
    > Public Property OriginalTVResolution() As en_TV_Resolution
    >
    > Get
    >
    > Return myTVResolution
    >
    > End Get
    >
    > Set(ByVal Value As en_TV_Resolution)
    >
    > myTVResolution = Value
    >
    > End Set
    >
    > End Property
    >
    >
    >
    >
    >
    >
    >
    > Public Sub AddNewTextBox(ByVal PosX As Integer, ByVal PosY As Integer,

    ByVal
    > TVWidth As Integer, _
    >
    > Optional ByVal InitText As String = "")
    >
    > If TVWidth = 0 Then
    >
    > Exit Sub
    >
    > End If
    >
    > Dim ctrl As New System.Web.UI.WebControls.TextBox
    >
    > Dim css As String = "POSITION: absolute; "
    >
    > Dim x As Integer = TVpx2PCpx_Width(PosX)
    >
    > Dim y As Integer = TVpx2PCpx_Height(PosY)
    >
    > Dim w As Integer = TVSize2PCSize_Width(TVWidth) 'TVpx2PCpx_Width(TVWidth)
    >
    > css &= "LEFT: " & x.ToString & "px; "
    >
    > css &= "TOP: " & y.ToString & "px;"
    >
    > ctrl.Attributes.Add("style", css)
    >
    > ctrl.Attributes.Add("width", w)
    >
    > ctrl.Width = Unit.Pixel(w)
    >
    > ctrl.Text = InitText
    >
    > ctrl.ID = CreateID(en_ctrlITVcontent_ChildControlType.TextBox)
    >
    > Me.Controls.Add(ctrl)
    >
    > myCOL.Add(ctrl)
    >
    > End Sub
    >
    >
    >
    > Public Sub AddNewDynamicTextBox(ByVal PosX As Integer, ByVal PosY As
    > Integer, ByVal TVSize_W As Integer, _
    >
    > ' ...
    >
    > ' like "AddNewTextBox"
    >
    > ' ....
    >
    > End Sub
    >
    >
    >
    >
    >
    > Public Sub AddNewImageButton(ByVal PosX As Integer, ByVal PosY As Integer,
    > ByVal TVSize As Byte, _
    >
    > ByVal PictureURL As String)
    >
    > ' ...
    >
    > ' like "AddNewTextBox"
    >
    > ' ...
    >
    > End Sub
    >
    >
    >
    > Public Sub RemoveAllChildControls()
    >
    > If HasControls() Then
    >
    > Dim i As Integer
    >
    > For i = Controls.Count - 1 To 0 Step -1
    >
    > Controls(i).Dispose()
    >
    > Next
    >
    > End If
    >
    > End Sub
    >
    >
    >
    > Private Function PAL_Width() As Integer
    >
    > Select Case OriginalTVResolution
    >
    > Case en_TV_Resolution.PAL_160x120 : Return 160
    >
    > Case en_TV_Resolution.PAL_320x240 : Return 320
    >
    > Case en_TV_Resolution.PAL_352x288 : Return 352
    >
    > Case en_TV_Resolution.PAL_352x576 : Return 352
    >
    > Case en_TV_Resolution.PAL_480x576 : Return 480
    >
    > Case en_TV_Resolution.PAL_720x576 : Return 720
    >
    > Case en_TV_Resolution.PAL_768x608 : Return 768
    >
    > End Select
    >
    > End Function
    >
    >
    >
    > Private Function PAL_Height() As Integer
    >
    > Select Case OriginalTVResolution
    >
    > Case en_TV_Resolution.PAL_160x120 : Return 160
    >
    > Case en_TV_Resolution.PAL_320x240 : Return 240
    >
    > Case en_TV_Resolution.PAL_352x288 : Return 288
    >
    > Case en_TV_Resolution.PAL_352x576 : Return 576
    >
    > Case en_TV_Resolution.PAL_480x576 : Return 576
    >
    > Case en_TV_Resolution.PAL_720x576 : Return 576
    >
    > Case en_TV_Resolution.PAL_768x608 : Return 608
    >
    > End Select
    >
    > End Function
    >
    >
    >
    > ' size 1=1/1 2=1/2 4=1/4 8=1/8 des fernse-bildes (und sogleich in PC-Pixel
    > ummappen)
    >
    > Private Function TVSize2PCSize_Width(ByVal TVSize As Integer) As Integer
    >
    > Return TVpx2PCpx_Width(PAL_Width() * (1 / TVSize))
    >
    > End Function
    >
    > Private Function TVSize2PCSize_Height(ByVal TVSize As Integer) As Integer
    >
    > Return TVpx2PCpx_Height(PAL_Height() * (1 / TVSize))
    >
    > End Function
    >
    > ' PosX [tv] = 240 => wird in diesem Ctrl auf PosX [pc] = 166 gemappt
    >
    > Private Function TVpx2PCpx_Width(ByVal TVPosX As Integer) As Integer
    >
    > Return CInt(TVPosX / (PAL_Width() / Val(Me.Width.ToString)))
    >
    > End Function
    >
    > Private Function TVpx2PCpx_Height(ByVal TVPosY As Integer) As Integer
    >
    > Return CInt(TVPosY / (PAL_Height() / Val(Me.Height.ToString)))
    >
    > End Function
    >
    >
    >
    > Private Function CreateID(ByVal ctrl_type As
    > en_ctrlITVcontent_ChildControlType) As String
    >
    > Dim id_nr As Integer = Me.Controls.Count
    >
    > Dim kz As String
    >
    > Select Case ctrl_type
    >
    > Case en_ctrlITVcontent_ChildControlType.TextBox : kz = "txtPreV" &
    > CStr(id_nr)
    >
    > Case en_ctrlITVcontent_ChildControlType.DynamicTextBox : kz =
    > "txtCtrlPreView" & (id_nr)
    >
    > Case en_ctrlITVcontent_ChildControlType.ImageButton : kz =

    "imgCtrlPreView"
    > & (id_nr)
    >
    > End Select
    >
    > Return kz
    >
    > End Function
    >
    >
    >
    > 'Protected Overrides Sub AddAttributesToRender(ByVal writer As
    > System.Web.UI.HtmlTextWriter)
    >
    > ''
    >
    >
    > 'End Sub
    >
    >
    >
    > ' invoke delegates registered with the click event.
    >
    > Protected Overridable Sub OnClick(ByVal e As EventArgs)
    >
    > RaiseEvent Click(Me, e)
    >
    > End Sub
    >
    > ' raise click event to owner of this control
    >
    > Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements
    > System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
    >
    > OnClick(New EventArgs)
    >
    > End Sub
    >
    >
    >
    > Protected Overrides Sub CreateChildControls()
    >
    > Me.InitializeComponent()
    >
    > End Sub
    >
    >
    >
    > Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
    >
    > MyBase.OnPreRender(e)
    >
    > End Sub
    >
    > Protected Overrides Sub Render(ByVal writer As

    System.Web.UI.HtmlTextWriter)
    >
    > Me.EnsureChildControls()
    >
    > MyBase.Render(writer)
    >
    > End Sub
    >
    > Private Sub InitializeComponent()
    >
    > If Me.Controls Is Nothing Then
    >
    > Dim ctrl As Control
    >
    > For Each ctrl In myCOL
    >
    > Me.Controls.Add(ctrl)
    >
    > Next
    >
    > End If
    >
    > End Sub
    >
    >
    >
    >
    >
    > Private Sub EventHandler_ImageClicked(ByVal sender As Object, ByVal e As
    > System.Web.UI.ImageClickEventArgs)
    >
    > Dim x As String
    >
    > x = ""
    >
    > End Sub
    >
    >
    >
    > Private Sub EventHandler_DynamicTextChanged(ByVal sender As Object, ByVal

    e
    > As System.EventArgs)
    >
    > Dim x As String
    >
    > x = ""
    >
    > End Sub
    >
    >
    >
    > Protected Overrides Sub RenderChildren(ByVal writer As
    > System.Web.UI.HtmlTextWriter)
    >
    > If HasControls() Then
    >
    > Dim i As Integer
    >
    > Dim c As New Control
    >
    > For i = Controls.Count - 1 To 0 Step -1
    >
    > Controls(i).RenderControl(writer)
    >
    > Next
    >
    > End If
    >
    > End Sub
    >
    > Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    >
    > '
    >
    > End Sub
    >
    > End Class
    >
    > End Namespace
    >
    >
    >
    >
    >
    > ############ CODE-END ############
    >
    >
     
    Victor Garcia Aprea [MVP], Jan 14, 2004
    #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. lupina
    Replies:
    3
    Views:
    27,236
    =?Utf-8?B?RWx0b24gVw==?=
    Mar 1, 2006
  2. Michael Lang
    Replies:
    12
    Views:
    922
    Michael Lang
    Dec 29, 2006
  3. =?Utf-8?B?Z29yaWxsYQ==?=
    Replies:
    1
    Views:
    789
    Masudur
    May 25, 2007
  4. lcplben
    Replies:
    4
    Views:
    120
  5. Bitswapper
    Replies:
    5
    Views:
    162
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page