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

Conquering Machu Picchu

"A life-long dream came true this morning, as I stood upon the mountain looking down at Machu Picchu. I felt like some strange mix between Indiana Jones and a small boy, gazing down upon the final treasure of the Incas."

Sessions in Seville

"I end my journey in front of the tomb of Columbus, located inside the world’s largest Gothic cathedral."

Alone in Kyoto

"I tried my best to sneak across the building, but the floor revealed me each time. I suppose I would make a lousy ninja."

Roaming the cobblestones of Istanbul

"Inside the church there are still traces of rune inscriptions made during the Viking age by Varangians, an elite guard made up of Scandinavian immigrant warriors."

The streets of Paris

"One of the advantages of repeated visits to a place is the leisure pace of awe and discovery while drifting slowly down the worn streets of the French capital."

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."