page.controls.add

jimmajsterski

Well-known member
Joined
Oct 29, 2004
Messages
53
Location
Houston, TX, USA
Programming Experience
10+
I'm new to asp.
Trying page.controls.add to add a control at runtime.
Using simple examples from MDSN and ASP.NET step by step.

This code is in the Page_Load Event of CompoundContainer.aspx.vb
Dim MyTextBox as New TextBox
Page.Controls.Add(MyTextBox)

When I browse CompoundContainer.aspx, I get this msg:

Exception Details: System.Web.HttpException: Control '_ctl0' of type 'TextBox' must be placed inside a form tag with runat=server

Must be a beginner mistake, but I am puzzled, any help out there?
Thanks.
 
Last edited:
It's just as the exception details say, you have to place server controls inside a form tag.
Every tutorial I have seen on the subject shows the code:
VB.NET:
Form1.Controls.Add(TextBox1)
Form1 is the id of the default Form control created when adding an aspx file, the Form control is rendered as a form tag.
See here for tutorial: HOW TO: Dynamically Create Controls in ASP.NET with Visual Basic .NET
 
Back
Top