I load LinkButton dynamically when a user clicks on another LinkButton. I attach an event handler to it. When a user clicks on a dynamically loaded LinkButton, the event does not fire.
From what I read, I understand this because when the page returns, the dynamically loaded control no longer exists. Looks like I have to make sure this control is recreated in Page_Init.
Dynamically created LinkButton depends on the value (product identifier). I need to somehow access this value so that I can correctly create the control. ViewState is unavailable and I am worried that I am using Session, this may lead to a timeout, and then it will not help. Any ideas?
In addition, I hard-coded the value of the product identifier for testing only, and this still did not raise the event. Is there anything else I need to do?
protected void Page_Init(object sender, EventArgs e)
{
SetTabText(1, 1);
}
SetTabText calls SetActionLinks, which creates a LinkButton:
protected Panel SetActionLinks(int prodID, int tabID) {
...
LinkButton lnkBtn = new LinkButton();
lnkBtn.ID = "lnkBtn" + rand.Next().ToString();
lnkBtn.CommandName = "action";
lnkBtn.Command += new CommandEventHandler(this.lnkAction_Command);
panel.Controls.Add(lnkBtn);
...
}
void lnkAction_Command(object sender, CommandEventArgs e)
{
LinkButton btn = (LinkButton)sender;
switch (btn.CommandArgument)
{
AddCart();
}
}
Sam
source
share