Dynamically add controls to a web form

  • Thread starter Thread starter Angel
  • Start date Start date
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
 
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
 
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.
 
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.
 
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
 
Back
Top