Some code i dug up from a project I did a year ago:
Code:
private void VullBestellingTabel(ArrayList bestelling) {
this.bestellingHeader_lbl.Visible = true;
this.bestelling_tbl.Rows.Clear();
double totaalPrijs = 0;
TableRow r;
TableCell c;
for (int i=0;i<bestelling.Count;i++) {
//hr toevoegen
if (i == 0) {
r = new TableRow();
c = new TableCell();
c.ColumnSpan = 4;
c.CssClass = "bestellingHeader";
c.Text = " ";
r.Cells.Add(c);
this.bestelling_tbl.Rows.Add(r);
}
Pizza p = (Pizza) bestelling[i];
totaalPrijs += p.Prijs;
//naam invullen
r = new TableRow();
c = new TableCell();
LinkButton rem = new LinkButton();
rem.ID = i.ToString();
rem.Click += new EventHandler(rem_Click);
rem.Text = "Verwijder pizza:";
c.Controls.Add(rem);
c.Wrap = false;
c.VerticalAlign = VerticalAlign.Bottom;
r.Cells.Add(c);
c = new TableCell();
c.Text = " € " + p.Prijs;
c.Wrap = false;
c.VerticalAlign = VerticalAlign.Bottom;
r.Cells.Add(c);
c = new TableCell();
c.Text = " - " + p.Pizzanaam;
c.Wrap = false;
c.VerticalAlign = VerticalAlign.Bottom;
r.Cells.Add(c);
c = new TableCell();
c.Text = " - topppings: " + p.Toppings;
c.Wrap = false;
c.VerticalAlign = VerticalAlign.Bottom;
r.Cells.Add(c);
this.bestelling_tbl.Rows.Add(r);
r = new TableRow();
c = new TableCell();
//Ruimte tussenlaten
c.Text = " ";
r.Cells.Add(c);
this.bestelling_tbl.Rows.Add(r);
}
if (bestelling.Count != 0) {
c = new TableCell();
r = new TableRow();
c.CssClass = "bestellingHeader";
c.Text = "Totaal:";
c.Font.Bold = true;
r.Cells.Add(c);
c = new TableCell();
c.Text = " € " + totaalPrijs.ToString();
c.CssClass = "bestellingHeader";
c.ColumnSpan = 3;
c.Font.Bold = true;
r.Cells.Add(c);
this.bestelling_tbl.Rows.Add(r);
}
this.send_mail_link.Visible = true;
}
I know it looks like a mess but anyhow: its a order page where the order is stored in a session var (passed by the variable called bestelling to the function).
I dynamically build the table, with the rows and cells, then add a Linkbutton (this can also be a normal button i guess).
Code:
LinkButton rem = new LinkButton();
rem.ID = i.ToString();
rem.Click += new EventHandler(rem_Click);
rem.Text = "Verwijder pizza:";
c.Controls.Add(rem);
This attaches an event to the linkbutton, for the event itself, to know which button was pressed I use the following code:
Code:
/// <summary>
/// De event die uitgevoerd wordt als er op een rem link geklikt wordt
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void rem_Click(object sender, EventArgs e) {
LinkButton s = (LinkButton)sender;
this.RemovePizza(Int32.Parse(s.ID));
this.BevolkToppingPrijsLijst();
}
So to bring this back to what you want to do: create a button for each row, set a property of the linkbutton (I used ID) according to the rowID.
Now in the eventhandler check for the sender object, get the ID from that object and you know which row to delete.
Note that there might be easier ways to build this table, by using DataSets etc... (I'm just giving you one way to do this, since every other way is kind of similar to this one).