Generating button controls on webform at runtime

clabarca

Newcomer
Joined
Aug 11, 2005
Messages
7
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
 
clabarca said:
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
 
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
 
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.
 
Back
Top