views:

1212

answers:

1

I have created a user control that will contain a table of LinkButtons (and other information). I create the table rows in another custom object and return a TableRowCollection with the link buttons in certain cells of these table rows.

In the code behind for the user control I created an event handler for the button click of these LinkButtons. I pass the user control into the method call to create the table rows (using "this") and then attempt to add the event handler to the "Click" of the LinkButton. For some reason, the event handler doesn't fire on a post back caused by this button. Any ideas as to what I could do to make this event fire?

Here is the code where I create the button and add the event handler:

LinkButton button = new LinkButton();
button.Text = movie.Title;
button.Click += new EventHandler(control.Link_Click);
button.CommandArgument = result.LocalID.ToString();
cell1.Controls.Add(button);

Where "control" is the user control passed in as a parameter and "Link_Click" is the event handler.

Thanks in Advance!

+1  A: 

First off, if the event handler is already inside the user control, there is no need to pass a reference to it into its own method. You can simply access any property or method inside the event handler.

Events rely on the control ID's to wire-up to the correct control. If you are not creating your linkbuttons exactly the same way every time on the postback, and ensuring that they have the same ID as they did before, then your event handler won't fire, because the ASP.Net pipeline won't be able to find what it thinks it the correct button in the control tree.

Additionally, if you are re-binding your user control to its datasource on every postback, this can cause events for some controls to get lost, for the same reason detailed above.

Check to make sure that you're recreating your controls properly every time, and that you're not re-binding your user control every time.

womp
Thanks! That helps. I think the issue is that I create these controls OnPreRender (after the event handlers fire, so the LinkButtons don't exist when the event handlers fire). I can't create the button's any sooner because I don't have the data for them sooner. I think I might have to think about how to do some sort of javascript workaround. Thanks again!
Nutzy
That would definitely be the issue :)
womp