You can create as many buttons as you need in Windows Forms.
Suppose that you already queried those values in your database and that you have them in a DataTable called dtButtonsToCreate with a single column, say 'fruits'.
To create as many buttons as needed do:
for (int i = 0; i < dtButtonsToCreate.Count; i++)
{
Button b = new Button();
b.Click += new EventHandler(OnButtonClick);
this.Controls.Add(b);
}
you need to have a method in advance for the event handler as seen above.
THIS NEEDS TO BE IN YOUR CODE IN ADVANCE (can't be created dynamically):
private void OnButtonClick(object sender, EvenArgs e)
{
}
BUT WAIT!
If you need different buttons, you must need them to do different things.
In this way every button would trigger the exact same action...
Let's solve that!
Create a new class called ExtendedButton and inherit from the button class:
using System.Windows.Forms
namespace blah
{
class ExtendedButton : System.Windows.Forms.Button
{
public ExtendedButton()
{ }
private string myval;
public string _myval
{
get{ return myval; }
set{ myval = value; }
}
}
}
Now do the same code you did at the beginning but with variation:
for (int i = 0; i < dtButtonsToCreate.Count; i++)
{
ExtendedButton b = new ExtendedButton();
b._myval = dtButtonsToCreate.Rows[i][0].ToString();
b.Click += new EventHandler(OnButtonClick);
this.Controls.Add(b);
}
There after, you can get the value of the button to perform anything you need in the event handler:
private void OnButtonClick(object sender, EvenArgs e)
{
string TheFruit = ((ExtendedButton)sender)._myval;
}
Hope this helps!
Should you need this in ASP.Net, you can pretty much do the same but adhere to the examples in this link:
http://msdn.microsoft.com/en-us/library/ms743596.aspx[
^]