troubleshooting Question

More on dynamically creating controls in VB.net

Avatar of urbnsurfr
urbnsurfr asked on
Visual Basic.NET
6 Comments1 Solution322 ViewsLast Modified:
Hi, I am new to VB.net development.  I am a moderately proficient programmer in Classic ASP.

In my classic ASP world, we have done a lot of work creating dynamic pages with control-specific settings (whether the control is present, editable, and a text box or combo box or whatever) that come out of databases and XML files.  I think we want to continue that approach in VB.net (you don't always know how many detail records will need to be editable at one time).

Therefore, I was reading this topic which seems relevant for getting started:
https://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_20717399.html
It references this topic:
http://www.codeproject.com/aspnet/RetainingState.asp

I converted the code to VB.net, and added a button control which calls the AddSomeControl method:
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        AddSomeControl()

    End Sub

When I attempt to run the solution, I get this error:
Server Error in '/TestWebApp' Application.
--------------------------------------------------------------------------------

Control 'ControlID_0' of type 'TextBox' must be placed inside a form tag with runat=server.

I have checked the HTML source design of the web form I am using and it most definitely is a runat=server.

Can you help me understand my error?  Thanks in advance!

In case it helps, this is the full code of my page:



Public Class WebForm1
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If (Page.IsPostBack) Then
            '//Controls must be repeatedly be created on postback
            'Me.createcontrols()
        Else
            '//Initiate the counter of dynamically added controls
            Me.NumberOfControls = 0
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        AddSomeControl()

    End Sub
    Private Property NumberOfControls()
        Get
            Return CInt(viewstate("NumControls"))

        End Get
        Set(ByVal Value)
            ViewState("NumControls") = Value
        End Set
    End Property

    Private Sub CreateControls()
        Dim count As Integer = Me.NumberOfControls
        Dim i As Integer
        Dim tx As TextBox
        For i = 1 To count
            tx = New TextBox
            tx.ID = "ControlID_" + i.ToString
            '//Add the Controls to the container of your choice
            Page.Controls.Add(tx)
        Next

    End Sub

    Private Sub AddSomeControl()
        Dim tx As TextBox = New TextBox
        tx.ID = "ControlID_" + Me.NumberOfControls.ToString
        Page.Controls.Add(tx)
        Me.NumberOfControls = Me.NumberOfControls + 1


    End Sub
End Class
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros