dynamic loading

khalik

Newcomer
Joined
Jan 24, 2003
Messages
1
hi

this is my first post in this forums... hope it get answerd soon..

can we load controls dymanic as we do in vb6.
and position them.

and is this possible in asp.net
 
Since this was in Windows Forms, here's an example....

Visual Basic:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim btn As New Button()
        Dim i As Integer

        For i = 1 To 10
            btn = New Button()
            With btn
                AddHandler .Click, AddressOf btnClick
                .Text = "button " & i
                .Location = New Point(0, i * 60)
                .Size = New Size(100, 50)
            End With
            Controls.Add(btn)
        Next
    End Sub

    Private Sub btnClick(ByVal sender As Object, ByVal e As EventArgs)
        MessageBox.Show("You clicked ... " & DirectCast(DirectCast(sender, Button).Text, String))
    End Sub
 
For ASP...
Visual Basic:
'top of page, inside your class...
    Private Panel1 As New System.Web.UI.WebControls.Panel()
    Private PlaceHolder1 As New System.Web.UI.WebControls.PlaceHolder()
    Private button1 As New System.Web.UI.WebControls.Button()
    Private label1 As New System.Web.UI.WebControls.Label()

'inside your Page_Load event...
    Controls.Add(PlaceHolder1)
    PlaceHolder1.Controls.Add(Panel1)
    Panel1.Controls.Add(label1)
    Panel1.Controls.Add(button1)
'then you can format the controls once they are added to the form
 
Thanks, Robby. This is exactly what I was looking for. I'm creating a new app for .NET using an old program concept I had and I needed the ability to add more controls when needed.

One question though, is what is a good method to later destroy them if not needed? For example, if the form only needs 5 at one time, then the user adds more by setting option, then decides they only really need 3 now, is there an easy way to identify the ones added to destroy?
 
you can use Controls.Removeat() or Controls.Remove(label1)

Or even the child of a container Panel1.Controls.Remove(label1)
 
Back
Top