Create controls dynamically: Getting an error

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi together
I wish to create an object at runtime in the web. I get many good tips for
how to
do before two days by using the same Subject. So I got also the following link
wich is showing exactly that what I want to do

http://www.codeproject.com/aspnet/retainingstate.asp

but, I got always an error
Das Steuerelement 'ControlID_0' des Typs 'TextBox' muss in einem Formtag mit runat=server positioniert werden.]

that means that the element 'ControlID_0' has to be positioned in a tag with
"runat=server".

Maybe my error is to understand that container


TextBox tx = new TextBox();
tx.ID = "ControlID_" + i.ToString();
//Add the Controls to the container of your choice
this.Controls.Add(tx);

Many thanks again if you know a further tip
Greetings Patrick Marti


PS:
I add this in an new Message bec. the other was two days before and will
maybe not be seen any more
 
What you are trying to do is add the textbox to the Page. Instead you need
to add the controls to an existing control which has the runat="server"
attribute which might be a form as in the following example:

// This works if you have a form tag on your page with id="Form1" and
// runat="server"
protected HtmlForm Form1;

private void Page_Load(object sender, System.EventArgs e)
{
TextBox box = new TextBox();
box.ID = "Textbox_1";
Form1.Controls.Add(box);
} }

HTH, Jakob.

Patrick Marti said:
Hi together
I wish to create an object at runtime in the web. I get many good tips for
how to
do before two days by using the same Subject. So I got also the following link
wich is showing exactly that what I want to do

http://www.codeproject.com/aspnet/retainingstate.asp

but, I got always an error
Das Steuerelement 'ControlID_0' des Typs 'TextBox' muss in einem Formtag mit runat=server positioniert werden.]

that means that the element 'ControlID_0' has to be positioned in a tag with
"runat=server".

Maybe my error is to understand that container


TextBox tx = new TextBox();
tx.ID = "ControlID_" + i.ToString();
//Add the Controls to the container of your choice
this.Controls.Add(tx);

Many thanks again if you know a further tip
Greetings Patrick Marti


PS:
I add this in an new Message bec. the other was two days before and will
maybe not be seen any more
 
Yes, my first dynamically object is born!!!
Thanks a lot!
Greetings Patrick

Jakob Christensen said:
What you are trying to do is add the textbox to the Page. Instead you need
to add the controls to an existing control which has the runat="server"
attribute which might be a form as in the following example:

// This works if you have a form tag on your page with id="Form1" and
// runat="server"
protected HtmlForm Form1;

private void Page_Load(object sender, System.EventArgs e)
{
TextBox box = new TextBox();
box.ID = "Textbox_1";
Form1.Controls.Add(box);
} }

HTH, Jakob.

Patrick Marti said:
Hi together
I wish to create an object at runtime in the web. I get many good tips for
how to
do before two days by using the same Subject. So I got also the following link
wich is showing exactly that what I want to do

http://www.codeproject.com/aspnet/retainingstate.asp

but, I got always an error
Das Steuerelement 'ControlID_0' des Typs 'TextBox' muss in einem Formtag mit runat=server positioniert werden.]

that means that the element 'ControlID_0' has to be positioned in a tag with
"runat=server".

Maybe my error is to understand that container


TextBox tx = new TextBox();
tx.ID = "ControlID_" + i.ToString();
//Add the Controls to the container of your choice
this.Controls.Add(tx);

Many thanks again if you know a further tip
Greetings Patrick Marti


PS:
I add this in an new Message bec. the other was two days before and will
maybe not be seen any more
 
Back
Top