clabarca Posted August 29, 2005 Posted August 29, 2005 Hello, I am trying to create buttons on a weform at runtime, based on entries from an .ini file. I can read the file just fine, but I am not sure how to dynamically create button controls on the form, or how to locate them in an orderly fashion. Should I use the repeater control? Should I use a datagrid? Can I just create a button right onto the form programatically? I know it can be done, I just don't know how to approach it. Thanks, Cesar Quote
JTDPublix Posted August 29, 2005 Posted August 29, 2005 Quote Hello, I am trying to create buttons on a weform at runtime, based on entries from an .ini file. I can read the file just fine, but I am not sure how to dynamically create button controls on the form, or how to locate them in an orderly fashion. Should I use the repeater control? Should I use a datagrid? Can I just create a button right onto the form programatically? I know it can be done, I just don't know how to approach it. Thanks, Cesar Cesar, This is the way I would approach it: 1.) Add an ASP:Panel "pnlButtons" to your form using HTML. 2.) From the code behind do the following: Dim myBtn as System.Web.UI.WebControls.Button Dim iniEntry as String = String.Empty Dim iniEntries() as String = String.Empty For Each iniEntry In iniEntries myBtn = New System.Web.UI.WebControls.Button myBtn.Text = iniEntry pnlButtons.Controls.Add(myBtn) Next Add any other attributes you need to add to myBtn before adding it to the panel's controls collection. Hope this helps! Jeff Quote
clabarca Posted August 29, 2005 Author Posted August 29, 2005 Thanks Jeff, worked beautifully. Just added a few spacers to make it look good and now I have dynamic button generation based on the contents of an .ini file. The problem I have now is... How do I handle the button click event? I am trying to use the CommandName and CommandArgument properties, but they apparently do nothing without having the OnCommand property set, and that is a protected property that I cannot access programatically. Is there a generic button click event that is raised whenever any button is clicked? Any ideas? Thanks, Cesar Quote
clabarca Posted August 30, 2005 Author Posted August 30, 2005 Never mind, I figured it out. Had to add the CommandName and CommandArgument properties to the buttons as I created them to make them command buttons, and then bubble the click event of the buttons to the panel. Worked perfectly. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.