Getting reference to Form object

  • Thread starter Thread starter Joo Park
  • Start date Start date
J

Joo Park

Hello,
I'm trying to add a TextBox to a web page dynamically.
However, the compiler is complaining that it needs to be
place in a form.

How do I get a reference to a form object
so that i can do the following?

Form.Controls.Add(textbox);

thx in advance!
 
If the web page does not have a form already, you can also add one
dynamically, and then use that to add controls.

By default the entire surface of an ASP.NET web form (Page) *is* an HTML
form, encapsulated by a Form object.
 
Add a placeholder control to the aspx page, instantiating the textbox contol
& add it to the panel.
TextBox1 = new TextBox();
Panel1.controls.add(TextBox1);
 
Sorry. I wrote placeholder as panel.
Add a placeholder control to the aspx page, instantiating the textbox contol
& add it to the placeholder.
TextBox1 = new TextBox();
Placeholder1.controls.add(TextBox1);
 
To get reference to the form id and add textbox using the reference try the
following code
foreach (Control ctrl in Page.Controls)
{
Type t = ctrl.GetType();
string str = t.FullName ;
if (str == "System.Web.UI.HtmlControls.HtmlForm")
{
HtmlForm frm =(HtmlForm)ctrl;
TextBox t5 = new TextBox();
t5.Text = "test..........";
frm.Controls.Add(t5);
}
}
 
Back
Top