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

Visions of Singapore

"By extensive use of landfill with sand bought from Indonesia and Cambodia, the bay area has expanded greatly in the last few years and tall buildings pop up everywhere."

Bunker safari

"The area contains 54 excavated bunkers along with gun emplacements, scattered around the town’s most visible landmark."

On the Trans-Siberian

"Since the toilets were locked during the seven hour stop, we had to bribe the provodnitsas to use the facilities. Then came the Mongols."

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

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

Roaming through Banff

"Even though the main town is surrounded by mountains with names such as Sulphur Mountain, it is as far away from Mordor as one could imagine."