Creating controls at runtime

G

geronimo

Hi,
I am trying to create a TextBox at runtime, using the following code with a
CodeBehind-File:

Dim txtBox As New System.Web.UI.WebControls.TextBox()
txtBox.ID ="txtTest"
Page.Controls.Add(txtBox)

I am getting an server-error - 'TextBox' has to be used with the FormTag
runat=server. Where can set the FormTag in a CodeBehind-File?

Could anyone help me?

Thank you

Geronimo
 
T

Teemu Keiski

It means TextBox is such control that needs to be inside server-side form at
ASP.NET Page.

By default ASPX page's control hierarchy is such that it has three controls
at first level.

Page.Controls[0] is a Literal control that has stuff before form tag
Page.Controls[1] is the HtmlForm that is the server-side form
Page.Controls[2] is a Literal control that has stuff after form tag

So you would need to either add the TextBox to the Form by:

Page.Controls[1].Controls.Add(txtBox)

or search the HtmlForm on Page's Controls. If it is for example:

<form id="Form1" method="post" runat="server">

You can add the TextBox also by:
Page.FindControl("Form1").Controls.Add(txtBox)

--
Teemu Keiski
MCP, Designer/Developer
Mansoft tietotekniikka Oy
http://www.mansoft.fi

AspInsiders Member, www.aspinsiders.com
ASP.NET Forums Moderator, www.asp.net
AspAlliance Columnist, www.aspalliance.com
 
G

geronimo

Dear Teemu,

Thank you for your help - it is working!

Regards

Geronimo




Teemu Keiski said:
It means TextBox is such control that needs to be inside server-side form at
ASP.NET Page.

By default ASPX page's control hierarchy is such that it has three controls
at first level.

Page.Controls[0] is a Literal control that has stuff before form tag
Page.Controls[1] is the HtmlForm that is the server-side form
Page.Controls[2] is a Literal control that has stuff after form tag

So you would need to either add the TextBox to the Form by:

Page.Controls[1].Controls.Add(txtBox)

or search the HtmlForm on Page's Controls. If it is for example:

<form id="Form1" method="post" runat="server">

You can add the TextBox also by:
Page.FindControl("Form1").Controls.Add(txtBox)

--
Teemu Keiski
MCP, Designer/Developer
Mansoft tietotekniikka Oy
http://www.mansoft.fi

AspInsiders Member, www.aspinsiders.com
ASP.NET Forums Moderator, www.asp.net
AspAlliance Columnist, www.aspalliance.com





geronimo said:
Hi,
I am trying to create a TextBox at runtime, using the following code
with
a
CodeBehind-File:

Dim txtBox As New System.Web.UI.WebControls.TextBox()
txtBox.ID ="txtTest"
Page.Controls.Add(txtBox)

I am getting an server-error - 'TextBox' has to be used with the FormTag
runat=server. Where can set the FormTag in a CodeBehind-File?

Could anyone help me?

Thank you

Geronimo
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top