Creating Form Controls at Run-Time

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
 
N

Natty Gur

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
 
T

Tarang Deshpande

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.
 
N

Natty Gur

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
 
T

Tarang Deshpande

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.
 
N

Natty Gur

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
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top