C#: Dynamically created Linkbuttons - how to add event handler?

Hi all.
I’ve got a bit of a problem… I create a couple of linkbuttons in a loop and I’m trying to get them to execute one line of code when they are clicked (server side). I was able to do this when the linkbuttons were hardcoded and dragged from the toolbox in design view… but when dynamically creating them… no go!

[size=2]LinkButton booklbtn;
[/size][size=2][color=#0000ff]for[/color][/size][size=2](i=0;i<prodfeatures.Length;i++) [/size]
[size=2]ProductionFeature feature = prodfeatures[i];[/size]
[size=2]booklbtn = [/size][size=2][color=#0000ff]new[/color][/size][size=2] LinkButton();[/size]
[size=2]booklbtn.Text = "book"; [/size]
[size=2]booklbtn.Click += [/size][size=2][color=#0000ff]new[/color][/size][size=2] System.EventHandler([/size][size=2][color=#0000ff]this[/color][/size][size=2].LinkButtons_Click);[/size]
[size=2]//"cell" is a html table cell to which I'm adding the link...[/size]

this works fine… the link button is created it the right place and is clickable.

now when the user clicks that… I want the following code to execute:

[size=2][color=#0000ff]private[/color][/size][size=2][color=#0000ff]void[/color][/size][size=2] LinkButtons_Click([/size][size=2][color=#0000ff]object[/color][/size][size=2] sender, System.EventArgs e)
PageController.Model.DoSearch("SomeText", DateTime.Parse("01-01-1900"), "", "");

when I click the linkbutton, however, the page is submitted and comes up blank (still the same page) and a breakpoint on that “pageController” line reveals that it never hits that line…

what am I missing?

Keep in mind that although the ASP.NET framework bends itself over to create the illusion of a form (like in WebForm), webpages are after all still a disconnected technology.

After a page has been rendered the server throws away the entire page instance. It creates the illusion that it was sitting there and waiting for your post-back by 1) recreating a new instance with the exact same structure and then 2) loading the viewstate that is posted back.

After that the Load event fires, with the IsPostBack set to true.

Now, this works fine for as long as the server is able to create a page with the same structure from the aspx page. But when you create controls dynamically the server has no way of knowing that on the postback (IMHO it could be done with some advanced programming involving reflection).

Hence, you must recreate the controls with the exact same IDs and set up the event listening also on the postback. If you fail to do that, nobody listens to the postback events.

My guess would be that this is where the problem lies.

If you create the controls by looping through a datareader or something similar, then one way to do it is to loop through the same data again on post-back. Another (more efficient way) would be to store information about which controls has been created somewhere in the viewstate, and recreate the controls from this info.

thanx for the reply.
actually just managed to sort the little bugger right before getting your message.

you’re close - in terms of your guess of where the problem lies!

I had an “IF” statement in my code to check if its a postback or not and some of the code was inside the “IF” and some outside… :rolleyes: aarghh!!

had me pulling my hair out for a couple of hours!!