Create a textbox dynamically and add it to the controls collection?

D

David

I try to use the following code to add a textbox
dynamically in a web form in the a .aspx.vb file:

Private Sub updateButton_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
updateButton.Click
Dim TextBox2 As New TextBox()
Controls.Add(TextBox2)

however, I got the error message like following when I
click the button updateButton:

Server Error in '/WebApplication1' Application.
-----------------------------------------------------------
---------------------

Control '_ctl0' of type 'TextBox' must be placed inside a
form tag with runat=server.
Description: An unhandled exception occurred during the
execution of the current web request. Please review the
stack trace for more information about the error and where
it originated in the code.

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


Stack Trace:


[HttpException (0x80004005): Control '_ctl0' of
type 'TextBox' must be placed inside a form tag with
runat=server.]
System.Web.UI.Page.VerifyRenderingInServerForm(Control
control) +152
System.Web.UI.WebControls.TextBox.AddAttributesToRender
(HtmlTextWriter writer) +38
System.Web.UI.WebControls.WebControl.RenderBeginTag
(HtmlTextWriter writer) +17
System.Web.UI.WebControls.TextBox.Render(HtmlTextWriter
writer) +17
System.Web.UI.Control.RenderControl(HtmlTextWriter
writer) +243
System.Web.UI.Control.RenderChildren(HtmlTextWriter
writer) +72
System.Web.UI.Control.Render(HtmlTextWriter writer) +7
System.Web.UI.Control.RenderControl(HtmlTextWriter
writer) +243
System.Web.UI.Page.ProcessRequestMain() +1900


please help me to solve the problem.

thank you!

David
 
C

Cor

Hi David,
The text is written, it says that the textbox has to be on the webform
(inside the tags).
I don't know how to do that dynamicly. I don't need it either.
You can place a textbox on a page, make it visible=false and then in the
click event make it visible=true.
Therefore I would not take time for another solution.
If you wants that, go and let me know?
If hope this solution helps anyway?
Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top