Mink Machine

Dynamic type creation in ASP.NET

ASP.NET web controls are usually littered in the markup in design time, but sometimes there is a need to dynamically add controls to an ASP.NET web page in runtime. Its quite easy to generate a server control from a string. The built-in types are stored in the GAC and requires a fully qualified strong name, such as:

“System.Web.UI.WebControls.Button, System.Web, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”;

This information can be found at c:\windows\assembly where you can view the properties of the assemblies. Once you got the strong name, the control can be created and eventually added to the controls collection of another control.

string s = “System.Web.UI.WebControls.Button, System.Web,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”;
Type t = Type.GetType(s);
Control c = (Control)Activator.CreateInstance(t);
f.Controls.Add(c);

Comments

No comments yet.

Post a comment

Your email address will not be published. Required fields are marked *

Featured stories

Wazzup in Vaduz

"Vaduz Castle is overlooking the town from a hill, a short walk from the center. It’s really a postcard view with the alps in the background, which I’m sure the prince enjoys as he sips his morning coffee while towering above his loyal subjects."

Road trip across the American Southwest

"We drove along Route 6, Route 66 and Route 666. If there was a Route 6666, we must have missed that turn."

Aliens in Nevada and New Mexico

"We turned around and went down another dirt road, past countless Joshua trees until our car was covered in dirt and we finally arrived at the Front Gate of Area 51."

The urban maze of Tangier

"There are exciting things waiting around every corner and my bucket list is long, but the Medina has a mindset of its own regarding time and space."

Chilling in Chile

"My weary feet has reached Valparaiso. After almost a month of traveling across the South American continent I realize that this is as far south as I will come, due to weather conditions."

The lakes of Lombardy

"The town of Como, largest settlement around Lake Como, attracts a lot of people with big wallets. But I preferred to take the funicular up the mountain to the Brunate region, where you get a much better view of the lake."