jamone
jamone

Reputation: 17421

ASP.NET LinkButton causes postback but doesn't call specified method

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?

Upvotes: 0

Views: 1597

Answers (3)

Lee Englestone
Lee Englestone

Reputation: 4669

I had a similar problem. Giving the LinkButtons an ID solved it for me.

Upvotes: 0

Tristan Warner-Smith
Tristan Warner-Smith

Reputation: 9771

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.

Upvotes: 1

Payton Byrd
Payton Byrd

Reputation: 986

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.

Upvotes: 2

Related Questions