1

I am dynamically creating link buttons on user control using a place holder and the eventhandler attached to the link button click+=new Event(Button_Click) is not firing

Thanks in Advance


Code Snippet of

   protected override void OnInit(EventArgs e)
             MenuListPlaceHolder.Controls.Add(new LiteralControl("<li>"));
             ctrl.ID = this.UniqueID + (nCounter++).ToString();
             ctrl.Text = cardType.Name;
             ctrl.Click += new EventHandler(this.CardName_Click);            
             MenuListPlaceHolder.Controls.Add(ctrl);
             MenuListPlaceHolder.Controls.Add(new LiteralControl("</li>"));

When clicked post back event is fired but not executing CardName_Click


rene
  • 41,474
  • 78
  • 114
  • 152
Madev
  • 46
  • 4

1 Answers1

0

You have to attach it on Page_Load event

like so:

protected void Page_Load(object sender, EventArgs e) {
    this.Btn.OnClick+=new Event(Button_Click) 
}

you should check it: here to learn about page cycle

hackp0int
  • 4,052
  • 8
  • 59
  • 95
  • based on your link the code from the OP should go in PreInit. OnInit fires before Page_load so I find it hard to believe your suggestion will work for OP – rene Dec 31 '11 at 14:09
  • Yes but PreInit you forgetting something you should attach delegate on each page request not once, so i'm speaking from my experience on that one. – hackp0int Dec 31 '11 at 18:57