Friday, August 6, 2010

How can I create a control dynamically in C# / ASP.NET?

All of the controls that you can drag onto the designer are objects that can be referenced and declared in code.





TextBox txt = new TextBox; // Declares a textbox control


txt.ForeColor = System.Drawing.Color.Red; // Assigns the text color to red





this.Controls.Add(txt); // Adds the textbox control created above to the page/form's control heirarchy.

No comments:

Post a Comment