Hi,
I am adding a LinkButton control dynamically into a PlaceHolder. If I add a new instance of the LinkButton on every OnInit(), then OnClick handler for this LinkButton works.
However, if I store the instance of LinkButton in the session and then add it into collection of controls of the PlaceHolder, LinkButton.OnClick fails to fire.
Is there a way to re-use controls I store in the session in given scenario? Recreating them every time is hardly an option.
Just as a side note - it's an inherited application in 1.1 which I just migrated to 3.5. The 'broken' approach seemed to work in 1.1.
Thanks for all the answers.
public static void Clicked(object sender, EventArgs e) {
Debugger.Break();
}
protected override void OnPreInit(EventArgs e) {
base.OnPreInit(e);
InitLinkButton();
}
private void InitLinkButton() {
var lb = new LinkButton();
plOne.Controls.Add(lb);
lb.ID = "lb";
lb.Text = "I will work.";
lb.Click += Clicked;
plOne.Controls.Add(lb);
LinkButton lb2 = null;
if (Session["lb2"] == null) {
lb2 = new LinkButton();
lb2.ID = "lb2";
lb2.Text = "I won't work.";
lb2.Click += Clicked;
Session["lb2"] = lb2;
} else {
lb2 = (LinkButton)Session["lb2"];
}
plOne.Controls.Add(lb2);
}