Well. I finally got it to work. Thanks to everybody who helped me. If you
are interested how to dynamically create controls on a website on click and
preserve their ViewState here your go:
Your aspx site should look something like this:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb"
Inherits="DynamicControlsAdd.WebForm1" trace = true %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1"
name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp

laceholder id="myPlaceHolder"
runat="server"></asp

laceholder><BR>
<BR>
<BR>
<asp:button id="btnAddTextBox" runat="server" Text="Add
Textbox"></asp:button>
</form>
</body>
</HTML>
And here is the code behind:
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 myPlaceHolder As
System.Web.UI.WebControls.PlaceHolder
Protected WithEvents btnAddTextBox 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
InitPage()
End Sub
Private Sub AddControls(ByVal strLabel As String)
Dim myNewTextBox As New System.Web.UI.WebControls.TextBox
' counter to give objects different names
counter = Session.Item("Counter")
counter += 1
Session.Item("counter") = counter
myNewTextBox.ID = String.Format("myNewTextBox{0}", counter)
myNewTextBox.Text = strLabel & CStr(counter)
myNewTextBox.EnableViewState = True
' save controls for SessionState
Dim dynControls As System.Collections.ArrayList
dynControls = Session.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 ControlTrip As New System.Web.UI.Triplet
ControlTrip.First = myNewTextBox.ID
ControlTrip.Second = myNewTextBox.GetType.ToString
ControlTrip.Third = myPlaceHolder.ID
dynControls.Add(ControlTrip)
' put control on Placeholder
myPlaceHolder.Controls.Add(myNewTextBox)
' and save information about control in SessionState
Session.Item("dynControls") = dynControls
End Sub
Private Sub btnAddTextBox_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnAddTextBox.Click
AddControls("Created From Button")
End Sub
Private Sub InitPage()
Dim SampleAssembly As System.Reflection.Assembly
' Load the assembly by providing the type name. Needs to be done
to instanciate the control
' with "CreateInstance" which needs a fully qualified name
Dim t As Type
t = GetType(System.Web.UI.WebControls.Label)
SampleAssembly =
System.Reflection.Assembly.Load(t.Assembly.FullNam e)
Dim dynControls As System.Collections.ArrayList
dynControls = Session.Item("dynControls")
If Not dynControls Is Nothing Then
Dim myEnumerator As IEnumerator
myEnumerator = dynControls.GetEnumerator
' run through all saved controls
While myEnumerator.MoveNext
Dim ctlTrip As System.Web.UI.Triplet
' restore Triplet containing informations about the
control
ctlTrip = CType(myEnumerator.Current,
System.Web.UI.Triplet)
Dim myPlaceHolder As
System.Web.UI.WebControls.PlaceHolder
myPlaceHolder = Page.FindControl(ctlTrip.Third)
Dim ctl As System.Web.UI.Control
Try
ctl =
Activator.CreateInstance(SampleAssembly.GetType(ct lTrip.Second, True))
ctl.ID = ctlTrip.First
Catch ex As Exception
Response.Write(ex.Message)
End Try
myPlaceHolder.Controls.Add(ctl)
End While
End If
End Sub
End Class
Thank you again Jacob and Teemu for your help!
Best regards
Daniel Walzenbach
P.S. If you need to contact me simply remove ".NOSPAM" from my email address
"Daniel Walzenbach" <> schrieb im
Newsbeitrag news:...
> Hi Jacob,
>
>
>
> Well, your example worked perfectly well which makes it more wired form e
as
> I thought I made exactly the same thing. I mean there must be a difference
> otherwise it would work but I can't see it. As it is late by now I'll look
> over it tomorrow in a more pronounced way.
>
>
>
> Thank you for your help! The code you gave me is a good basis for a start
>
At least I know by now what does work.
>
>
>
> Daniel
>
>
>
>
>
> "Jacob Yang [MSFT]" <> schrieb im Newsbeitrag
> news:...
> > Hi Daniel,
> >
> > Firstly I want to thank Teemu's great help on this issue. Please refer
to
> > his response carefully.
> >
> > From your code snippet, I am not sure how you assign the values to those
> > dynamical controls.
> >
> > The postback data processing and the viewstate restore were done
> > automatically by page in each request.
> >
> > One important thing is that those dynamical controls should be added to
> the
> > control tree in the same order in each request, including the first
> > request. Otherwise, the viewstate will fail to work.
> >
> > Please try the following code, checking whether it works fine,
> >
> > 1. Create a new ASP.NET (VB.NET) app,
> > 2. Webform1.aspx will be added be default.
> > 3. Drap and drop a button onto the web from.
> > 4. Copy the following code into the Page_Load event.
> >
> > 'Put user code to initialize the page here
> > Dim l As Label = New Label
> > Page.Controls(1).Controls.Add(l)
> >
> > Dim txt As TextBox = New TextBox
> > Page.Controls(1).Controls.Add(txt)
> >
> > If (Not IsPostBack) Then
> > l.Text = "Set on initial request"
> > End If
> >
> > 5. Compile and browse the webform1.aspx.
> > 6. Type any chars into the textbox that was added dynamically.
> > 7. Click the button to submit the webform. When the submit completed,
you
> > should see the value you typed in the textbox should persist, and also
the
> > text of the Label control should also be there.
> >
> > Please try it and let me know the result.
> >
> > Best regards,
> >
> > Jacob Yang
> > Microsoft Online Partner Support
> > Get Secure! ¨C www.microsoft.com/security
> > This posting is provided "as is" with no warranties and confers no
rights.
> >
>
>