views:

97

answers:

2

I have this method defined since I will add a link button to each row in a long table. However it does not call the eventHandler. It does cause a asyncPostback but never calls the eventHandler.

public static void InsertLinkButton(string text, string id, EventHandler eventHandler,
        UpdatePanel updateSummary, PlaceHolder placeHolder)
{
    LinkButton link = new LinkButton();
    link.Text = text;
    link.Click += eventHandler;
    link.CausesValidation = false;
    AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
    trigger.ControlID = link.ID = "link" + id;
    trigger.EventName = "Click";
    Utils.Tag(link, placeHolder);
    updateSummary.Triggers.Add(trigger);
}

The event handler's signiture:

protected void link_Click(object sender, EventArgs e)
{
    //check which link clicked me and do stuff
}

I would call the method with this:

InsertLinkButton("Division", "Division", link_Click, updateSummary, placeHolderSummary);

Is something in this code wrong? Or is my problems elseware?

+2  A: 

You need to make sure your button has been inserted to the page before the click event is handled, otherwise the postback has no work to do.

Payton Byrd
+1  A: 

Are you calling your insert early enough in the page lifecycle?

If you're wiring the click event up too late, you'd never have the event raised.

Try moving the call into the Page Init.

TreeUK