5
Answers

Passing data from a panel to a method

D Green

D Green

9y
632
1
I created a panel using dynamic controls. Within the foreach statement, a few rows of label: Name and Price with a Update and Remove button for each row  are created.  I'm happy so far.  When the user clicks on the Remove button for a particular row, how do i get the correct row to be deleted.  I noticed a _doPostBack is automatically created for each row but I do not know how to use the doPostBack to delete the row in question.  Do I create another label (lblrow) that stores a number for each row created?  If so how, do i pass the lblrow to Remove button method? Thank you for your assistance in advance.
 
foreach (Cart cartlist in cart)
{
int crtId = cartlist.ID;

Panel cartPanel = new Panel();
Label lblName = new Label();
lblName.Text = product.Name;
lblName.CssClass = "atcName";

Label lblPrice = new Label
{
Text = String.Format("{0:c}", product.Price),
CssClass = "atcPrice"
};


totalCounter++; //totalCounter = totalCounter + 1

Button lkbUpdate = new Button
{
CssClass = "lkbUpdate",
Text = "Update"
};
lkbUpdate.Click += new EventHandler(lkbUpdate_Click);

LinkButton lkbRemove = new LinkButton
{
CssClass = "lkbRemove",
Text = "Remove",
};
lkbRemove.Click += new EventHandler(lkbRemove_Click);

//Add child controls to Panel
cartPanel.Controls.Add(lblName);
cartPanel.Controls.Add(new Literal { Text = "<br />" });
cartPanel.Controls.Add(lblPrice);
cartPanel.Controls.Add(new Literal { Text = "<br />" });
cartPanel.Controls.Add(lkbUpdate);
cartPanel.Controls.Add(lkbRemove);


//Add dynamic Panels to static Parent panel
pnlCart.Controls.Add(cartPanel);
}
 
 
protected void lkbRemove_Click(object sender, EventArgs e)
{
  ....code here...
}
 
Answers (5)