Dynamically Creating Controls at Runtime

M

Mike Towle

Trying to dynamically create and bind (to a form) controls at runtime in the button_click event, not in Page_Init. If I create and bind the controls in Page_Init, they work fine. If I try doing this in the button_click event several things happen
1. The control.count property of the container that the control was added to (usually a form) does not change
2. I loose control of that control. I cannot clear the control or dispose of it on subsiquent calls to the webform. For instance if I dynamically create a dropdownlist control, I cannot get rid of it and I cannot clear the items list on the next call to the webform

How can I dynamically create and dispose controls in runtime that I can control on subsiquent calls to the webform? This application makes calls to a database and the control typs that are created depend upon what the values are in the database. One time I might use a textbox control because the value in the database is 'T' while another time I might use a dropdownlist if the value is 'D'.
 
A

Alvin Bruney [MVP]

try this beauty: customize to your liking. this snippet will not compile

Type type = Type.GetType("System.Web.UI.WebControls." +
row.ObjectType + ", System.Web,Version=1.0.3300.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a");
System.Web.UI.Control temp =
(System.Web.UI.Control)Activator.CreateInstance(type);
 

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,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top