Reputation: 41
My problem is that I dynamically create a table where each cell includes a linkbutton, that when clicked is supposed to remove that cell from the table. (It is i bit more complex than that but I will not go into those details, just saying that a workaround will not do) I have read a few post on this and it usually is mentioned that the control has too be (re)made in page load or before. I have tried to run the method that runs setCellContent from both page load and page init and pre init but the _lnkBntRemoveSlotFromTable_Click method is never called as the linkbuttons are clicked. And I am beginning to wonder there is something else wrong than just when the controls are created/recreated.
For each cell the table consist of, this is what is done:
private TableCell setCellContent(string day, DateTime timeOfDay){
TableCell newCell = new TableCell();
LinkButton lb = new LinkButton();
lb.ID = (++global_counter_id).ToString();
lb.Text = timeOfDay.ToShortTimeString();
lb.CommandArgument = timeOfDay.ToString();
lb.Command += new CommandEventHandler(_lnkBntRemoveSlotFromTable_Click);
newCell.Controls.Add(lb);
return newCell;
}
The method I want to be called:
public void _lnkBntRemoveSlotFromTable_Click(object sender, CommandEventArgs e)
{
//1. Make changes to the table
}
But the method is never called.
Upvotes: 3
Views: 3307
Reputation: 41
Finally got it working. A few changes made it work. It had ofcourse to do with when the table was created and how the id was created. For advice for others here is an example of when it works.. And make sure the ID of the dynamic control stays the same across page loads.
public partial class _default : System.Web.UI.Page
{
static int i = 0;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
LinkButton lb = new LinkButton();
lb.ID = "id";
lb.Text = "Click me";
lb.CommandArgument = "argument";
lb.Command += new CommandEventHandler(method_to_call);
this.Panel.Controls.Add(lb);
}
private void method_to_call(object sender, CommandEventArgs e)
{
i++;
this.Label.Text = i.ToString();
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
Upvotes: 1