Dynamically add controls to a web form

A

Angel

Can one add a serverside control to a webform
dynamically? I used in the Page_load event of the form
the following code:

Dim txt as new System.Web.UI.Webcontrols.Textbox()
Controls.add(txt)

The code fails. Does this mean you just cannot in
Webforms where you CAN in Windows Forms?

Thanks
 
S

S. Justin Gengo

Angel,

You can certainly add controls dynamically.

What is the error message?

Sincerely,

--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 
S

SSW

U need to have a Place holder or Panel to add control.

Dim txt as new System.Web.UI.Webcontrols.Textbox()
PHtxt.Controls.add(txt)// where PHtxt is PlaceHolder web controls

Thanks,

sswalia
MCSD, MCAD, OCA.
 
B

Bret Mulvey [MS]

SSW said:
U need to have a Place holder or Panel to add control.

Dim txt as new System.Web.UI.Webcontrols.Textbox()
PHtxt.Controls.add(txt)// where PHtxt is PlaceHolder web controls

No, you shouldn't need a placeholder or panel. I add controls to pages all
the time without placeholders.

Some pages don't have a Controls collection at all. Maybe that's the
problem. If a page uses <% ... %> code blocks mixed with HTML (the old ASP
style) then the whole page ends up as a bunch of Response.Write statements
instead of having a Controls collection with LiteralControl objects
interleaved with other ASP.NET controls.
 
S

SSW

So it's alway safe to use placeholder/Panel to dunamically added controls.
Loaction is know at desing time.

I prefer using panel/Pace holder and it should work for all the case.

Thanks,

sswalia
MCSD, MCAD, OCA
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top