Creating Form Controls at Run-Time

  • Thread starter Thread starter Tarang Deshpande
  • Start date Start date
T

Tarang Deshpande

I would like to be able to create controls at run-time
rather than at design time. Is there a function that I
can override so that I can output the appropriate html
code between when the <body> and </body> tags are outputed.

I don't have to have web form controls but could just be
plain html controls.

Thanks
 
You can create server controls or Html controls at the page_load event.
you can also attach them to events :

System.Web.UI.WebControls.ListBox oLst = new
System.Web.UI.WebControls.ListBox();
oLst.Enabled = true;
oLst.EnableViewState = true;
oLst.ID = "cboDevQueueList";
oLst.AutoPostBack = true;
oLst.Items.Add ("a");
oLst.Items.Add ("b");
oLst.Visible = true;
oLst.SelectedIndexChanged += new System.EventHandler(this.SelItem);
// ad the control inside the form
("WebForm6").Controls.Add(oLst);

Natty Gur, CTO
Dao2Com Ltd.
28th Baruch Hirsch st. Bnei-Brak
Israel , 51114

Phone Numbers:
Office: +972-(0)3-5786668
Fax: +972-(0)3-5703475
Mobile: +972-(0)58-888377

Know the overall picture
 
How about if you want to create html controls rather than
web controls what do you do? I tried using Response.Write
during the Page_Load but that only puts the html code
before everything else from the ASPX page.
 
The same way and location just use Html control classes:

System.Web.UI.HtmlControls.HtmlInputText o = new
System.Web.UI.HtmlControls.HtmlInputText ();
o.Value = "natty";
o.Visible = true;
this.FindControl("WebForm6").Controls.Add(o);

Natty Gur, CTO
Dao2Com Ltd.
28th Baruch Hirsch st. Bnei-Brak
Israel , 51114

Phone Numbers:
Office: +972-(0)3-5786668
Fax: +972-(0)3-5703475
Mobile: +972-(0)58-888377

Know the overall picture
 
This doesn't quite work. I added label controls and if
you look at the gnerated html code at the client the end
result is rendered after the </HTML> tag and thus outside
of the form. Also each label was between <span></span>
tags.
 
Sorry, I have mistake at this line :>("WebForm6").Controls.Add(oLst);

this.controls["YourFormName"].Controls.Add(oLst);

you need to use the right Controls collection to add your control to.

Natty Gur, CTO
Dao2Com Ltd.
28th Baruch Hirsch st. Bnei-Brak
Israel , 51114

Phone Numbers:
Office: +972-(0)3-5786668
Fax: +972-(0)3-5703475
Mobile: +972-(0)58-888377

Know the overall picture
 
Back
Top