Dynamically Create Controls in ASP.NET with Visual Basic .NET

Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents Label4 As System.Web.UI.WebControls.Label
    Protected WithEvents Label3 As System.Web.UI.WebControls.Label
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label

    ' Added by hand for access to the form.
    Protected Form1 As System.Web.UI.HtmlControls.HtmlForm

    ' Added by hand; will create instance in OnInit.
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox

 

Add code to create the TextBox controls dynamically. The controls are created every time that the page is run. The best place to do this is in the Page_Init function that the WebForm1 class provides. Locate the Page_Init function. Expand the code that is marked with the comment "Web Form Designer generated code." Modify the Page_Init functions, so that they appear similar to the following:

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    ' Create dynamic controls here.
    TextBox1 = New TextBox()
    TextBox1.ID = "TextBox1"
    TextBox1.Style("Position") = "Absolute"
    TextBox1.Style("Top") = "25px"
    TextBox1.Style("Left") = "100px"
    Form1.Controls.Add(TextBox1)

    TextBox2 = New TextBox()
    TextBox2.ID = "TextBox2"
    TextBox2.Style("Position") = "Absolute"
    TextBox2.Style("Top") = "60px"
    TextBox2.Style("Left") = "100px"
    Form1.Controls.Add(TextBox2)

    ' CODEGEN: The Web Form Designer requires this method call.
    ' Do not modify it by using the code editor.
    InitializeComponent()
End Sub

 

This code dynamically creates two TextBox controls, sets their IDs and positions, and then binds them to the Form Controls collection. You can also add Web Forms Panel controls to the ASPX page, and then bind the text boxes to those controls in the Page_Init function, as in the following example:

    TextBox1 = New TextBox()
    TextBox1.ID = "TextBox1"
' comment   add command  the Form Controls collection as follows
'   Form1.Controls.Add(TextBox1)'
    Panel1.Controls.Add(TextBox1)

 

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Not IsPostBack Then
        ' Set the initial properties for the text boxes.
        TextBox1.Text = "TextBox1"
        TextBox2.Text = "TextBox2"
    End If
End Sub

And the Event

Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
    Dim txtBoxSender As TextBox
    Dim strTextBoxID As String

    txtBoxSender = CType(sender, TextBox)
    strTextBoxID = txtBoxSender.ID

    Select Case strTextBoxID
        Case "TextBox1"
            Label3.Text = "TextBox1 text was changed"

        Case "TextBox2"
            Label4.Text = "TextBox2 text was changed"
    End Select
End Sub

Source

Posted by:

Comments History


by: on
Hello! ddgekeg interesting ddgekeg site! I'm really like it! Very, very ddgekeg good!
 
by: on
Very nice site!
 

Name :  

Email :  

Comment Below