Dynamic Web Control Generation: Breaking Old Habits
Ah, for the days of classic ASP I thought early today. I need to dynamically generate some things from the server side so my old habits kick in and I thinks to meself:
<% For i=0 to 10 Response.Write("<input id='dyn" & i & "'... >") Next %>
Doesn't quite work with ASP.NET. "Grrrr" I think to myself until I dig up an old post from The Code Project on the very subject. Two seconds and I realize ASP.NET is not only easier but more robust. For dynamic controls:
1. Use a placeholder control somewhere on your page: <asp:PlaceHolder id="afronaut" Runat="server" />
2. Use some C# to access and build the controls collection of your placeholder: private void Page_Load(object sender, EventArgs e){ for(int i=0; i<3;i++){ TextBox tx = new TextBox(); afronaut.Controls.Add(tx); } }
Once again it is proved that ASP.NET, while breaking old habits, surely is more robust. Probably the nicest thing I can think of right now is that it will work well with User Controls which are much more sophisticated than anything you could muster with the traditional Response.Write.
9:21:43 PM
|