Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > user controls: dynamiclly added child controls dont survive post back ?

Reply
Thread Tools

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

 
 
Wolfgang Brucker
Guest
Posts: n/a
 
      01-14-2004
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.PermissionSetAttribut e(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.TextBo x)

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.RaisePostBackE vent

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 ############


 
Reply With Quote
 
 
 
 
Victor Garcia Aprea [MVP]
Guest
Posts: n/a
 
      01-14-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.PermissionSetAttribut e(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.TextBo x)
>
> 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.RaisePostBackE vent
>
> 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 ############
>
>



 
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
How can added attributes survive a page reload? lcplben Javascript 4 10-25-2009 09:18 PM
ASP.NET 2.0 - Losing controls added in post back Michael Lang ASP .Net 12 12-29-2006 12:34 AM
GridView dynamiclly adding TemplateField lupina ASP .Net 3 03-01-2006 01:16 AM
HELP! Child controls of child controls not visible to web app lisa@starways.net ASP .Net Building Controls 0 05-05-2005 09:14 PM
how to alloct dynamiclly with a class Nikki C++ 3 01-27-2004 07:11 PM



Advertisments