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 = "
" });
cartPanel.Controls.Add(lblPrice);
cartPanel.Controls.Add(new Literal { Text = "
" });
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...
}
5 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Piyush PansuriyaPosted Jan 8, 2015, 12:31 AM
For that, put break point on click event and check where did you get object of Cart in sender
Piyush PansuriyaPosted Jan 9, 2015, 12:13 AM
Ya sure.! My name is Piyush
D GreenPosted Jan 8, 2015, 7:02 PM
D GreenPosted Jan 7, 2015, 4:46 PM
using System.Data.Linq
unfortunately, the .DataContext did not come up as part of the Linq Reference.Piyush PansuriyaPosted Jan 7, 2015, 12:31 AM
protected void lkbRemove_Click(object sender, EventArgs e)
{
var obj = (sender as LinkButton).DataContext as Cart;
cart.DeleteObject(obj);
}
If it is diffrent from my code, you can check what will you get in sender when this event will call. As per that you can try.