Ask a Question related to ASP.NET Building Controls, Design and Development.
-
Wolfgang Brucker #1
user controls: dynamiclly added child controls dont survive post back ?
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 ############
Wolfgang Brucker Guest
-
Composite Web Control and saving user changes on child controls.
Okay, having the same problem many others are having. Here is a simple example I cannot get to work: -- using C# a) Created an ASP.NET Web... -
Control.Controls bug? Control's child controls missing at the run time.
Hello, ..NET 1.1/VB.NET: I have a custom web control Public Class DatePicker Inherits Control Implements INamingContainer -
Custom Composite Control, child User-controls loosing view state
I've created composite custom control which dynamically creates and arranges it's child controls based on some xml data. Beside the simple web form... -
Accessing Properties of Custom Controls child Controls
I am using a Custom Control on a page which renders a button control if required. I need to access the child button control's properties (i.e.... -
How To Get Client Added Controls back to Server
Hello, Using Java Script I add some ListItem items to my ListBox ( or Options to my list box ) when I post back I don't get anything at all... -
Victor Garcia Aprea [MVP] #2
Re: user controls: dynamiclly added child controls dont survive post back ?
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:
[url]http://obies.com/vga/blog.aspx[/url]
To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
"Wolfgang Brucker" <b.d.s.@gmx.at> wrote in message
news:40051818@e-post.inode.at...add's> hi,
>
> i have some strange behaviour: i've created a web user control thatcollection> some child controls (e.g: textbox, image buttons) to its control<System.Security.Permissions.PermissionSetAttribut e(System.Security.Permissi> (= 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
>
>
>
>ByVal> 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,"imgCtrlPreView"> 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 =System.Web.UI.HtmlTextWriter)> & (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 Ase>
> 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> 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] Guest



Reply With Quote

