D
Daniel Walzenbach
Hi everybody,
I need to dynamically populate a webpage at runtime with controls. This is the code I wrote.
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Vom Web Form Designer generierter Code "
'Dieser Aufruf ist für den Web Form-Designer erforderlich.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents PlaceHolder1 As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
'HINWEIS: Die folgende Platzhalterdeklaration ist für den Web Form-Designer erforderlich.
'Nicht löschen oder verschieben.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: Dieser Methodenaufruf ist für den Web Form-Designer erforderlich
'Verwenden Sie nicht den Code-Editor zur Bearbeitung.
InitializeComponent()
End Sub
#End Region
Private counter As System.Int32 = 0
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Hier Benutzercode zur Seiteninitialisierung einfügen
End Sub
Private Sub AddControls(ByVal strLabel As String)
Dim myNewLabel As New System.Web.UI.WebControls.Label
' counter to give objects different names
counter = Session.Item("Counter")
counter += 1
Session.Item("counter") = counter
myNewLabel.ID = String.Format("myNewLabel{0}", counter)
myNewLabel.Text = strLabel & CStr(counter)
' I thought that EnableViewState should be sufficient to store the controls value
myNewLabel.EnableViewState = True
' save controls for ViewState
Dim dynControls As System.Collections.ArrayList
dynControls = Viewstate.Item("dynControls")
' create new ArrayList if it does not exist
If dynControls Is Nothing Then
dynControls = New System.Collections.ArrayList
End If
'store information about control in Triplet
Dim labelTrip As New System.Web.UI.Triplet
labelTrip.First = myNewLabel.ID
labelTrip.Second = myNewLabel.GetType.ToString
labelTrip.Third = PlaceHolder1.ID
dynControls.Add(labelTrip)
' put control on Placeholder
PlaceHolder1.Controls.Add(myNewLabel)
' and save information about control in ViewState
ViewState("dynControls") = dynControls
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AddControls("Created From Button")
End Sub
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
Dim SampleAssembly As System.Reflection.Assembly
Dim t As Type
t = GetType(System.Web.UI.WebControls.Label)
SampleAssembly = System.Reflection.Assembly.Load(t.Assembly.FullName)
Dim dynControls As System.Collections.ArrayList
dynControls = ViewState.Item("dynControls")
If Not dynControls Is Nothing Then
' run through all saved controls
For Each obj As System.Object In dynControls
Dim ctlTrip As System.Web.UI.Triplet
ctlTrip = CType(obj, System.Web.UI.Triplet)
Dim parent As System.Web.UI.WebControls.PlaceHolder
parent = Page.FindControl(ctlTrip.Third)
Dim ctl As System.Web.UI.Control
Try
'ctl = Activator.CreateInstance(Type.GetType(ctlTrip.Second, True))
ctl = Activator.CreateInstance(SampleAssembly.GetType(ctlTrip.Second, True))
Catch ex As Exception
Response.Write(ex.Message)
End Try
ctl.ID = ctlTrip.First
parent.Controls.Add(ctl)
Next
End If
End Sub
End Class
The controls get recreated in the OnLoad method but don't contain any values. I thought their values should be stored in ViewState? Does anybody know why the controls don't contain any values?
Thanks in advance
Best regards
Daniel Walzenbach
P.S. If you need to contact me simply remove ".NOSPAM" from my email address.
I need to dynamically populate a webpage at runtime with controls. This is the code I wrote.
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Vom Web Form Designer generierter Code "
'Dieser Aufruf ist für den Web Form-Designer erforderlich.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents PlaceHolder1 As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
'HINWEIS: Die folgende Platzhalterdeklaration ist für den Web Form-Designer erforderlich.
'Nicht löschen oder verschieben.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: Dieser Methodenaufruf ist für den Web Form-Designer erforderlich
'Verwenden Sie nicht den Code-Editor zur Bearbeitung.
InitializeComponent()
End Sub
#End Region
Private counter As System.Int32 = 0
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Hier Benutzercode zur Seiteninitialisierung einfügen
End Sub
Private Sub AddControls(ByVal strLabel As String)
Dim myNewLabel As New System.Web.UI.WebControls.Label
' counter to give objects different names
counter = Session.Item("Counter")
counter += 1
Session.Item("counter") = counter
myNewLabel.ID = String.Format("myNewLabel{0}", counter)
myNewLabel.Text = strLabel & CStr(counter)
' I thought that EnableViewState should be sufficient to store the controls value
myNewLabel.EnableViewState = True
' save controls for ViewState
Dim dynControls As System.Collections.ArrayList
dynControls = Viewstate.Item("dynControls")
' create new ArrayList if it does not exist
If dynControls Is Nothing Then
dynControls = New System.Collections.ArrayList
End If
'store information about control in Triplet
Dim labelTrip As New System.Web.UI.Triplet
labelTrip.First = myNewLabel.ID
labelTrip.Second = myNewLabel.GetType.ToString
labelTrip.Third = PlaceHolder1.ID
dynControls.Add(labelTrip)
' put control on Placeholder
PlaceHolder1.Controls.Add(myNewLabel)
' and save information about control in ViewState
ViewState("dynControls") = dynControls
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AddControls("Created From Button")
End Sub
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
Dim SampleAssembly As System.Reflection.Assembly
Dim t As Type
t = GetType(System.Web.UI.WebControls.Label)
SampleAssembly = System.Reflection.Assembly.Load(t.Assembly.FullName)
Dim dynControls As System.Collections.ArrayList
dynControls = ViewState.Item("dynControls")
If Not dynControls Is Nothing Then
' run through all saved controls
For Each obj As System.Object In dynControls
Dim ctlTrip As System.Web.UI.Triplet
ctlTrip = CType(obj, System.Web.UI.Triplet)
Dim parent As System.Web.UI.WebControls.PlaceHolder
parent = Page.FindControl(ctlTrip.Third)
Dim ctl As System.Web.UI.Control
Try
'ctl = Activator.CreateInstance(Type.GetType(ctlTrip.Second, True))
ctl = Activator.CreateInstance(SampleAssembly.GetType(ctlTrip.Second, True))
Catch ex As Exception
Response.Write(ex.Message)
End Try
ctl.ID = ctlTrip.First
parent.Controls.Add(ctl)
Next
End If
End Sub
End Class
The controls get recreated in the OnLoad method but don't contain any values. I thought their values should be stored in ViewState? Does anybody know why the controls don't contain any values?
Thanks in advance
Best regards
Daniel Walzenbach
P.S. If you need to contact me simply remove ".NOSPAM" from my email address.