views:

2295

answers:

2

I have a nested UserControl (this control is dynamicall loaded by another UserControl that is dynamically loaded by an aspx page inside a MasterPage) in which I would like to use a LinkButton and the OnCommand Event.

This button must be added to some panel, so I hooked up to the OnLoad event of the panel (it needs to be created before events are fired in the lifecycle) :

protected void PatentAssignee_Load(object sender, EventArgs e) {
    Label label = (Label)sender;

    LinkButton link = new LinkButton();
    link.Text = name;
    link.Command += Link_OnCommand;
    link.CommandArgument = "argument";
    link.ID = "someID";

    label.Controls.Add(link)
}

protected void Link_OnCommand(object sender, CommandEventArgs e) {
    Response.Write(e.CommandArgument);
}

But I can't get the Link_OnCommand method to be called. One more thing that my be relevant to this problem : the UserControl is inside an UpdatePanel. I also tried to make the link trigger a full postback :

ScriptManager s = (ScriptManager)this.Page.Master.FindControl("__scriptManager");
s.RegisterPostBackControl(link);

... but it doesn't change much, the page is fully reloaded but no event is fired.

EDIT: As requested in the comments, here are more details about the nesting :

MasterPage
  PlaceHolder
    Page
      UpdatePanel
        UserControl1
          FormView
            PlaceHolder
              UserControl2
                LinkButton

This means that UserControl2 is dynamically loaded.

A: 

I remember having this problem as well. I didn't find the real problem, but what I did, was hook up the eventhandler in the aspx markup and it worked.

You might also try to hook the eventhandler in some earlier stage of the Page Life Cycle.

Try the PreInit eventhandler.

More info on the Page Life Cycle here.

You mean creating the LinkButton in the aspx instead of on the server side ? How could I do that dynamically, ie with CommandArgument dependent on some data ?
Wookai
I'm sorry, my bad :) I thought you were only adding the eventhandler dynamically. I do think dynamically creating controls should be done earlier in the Life Cycle.
I also tried to do it during the Init... But I think the problem is elsewhere, as I also tried to add a LinkButton manually to my page, and the event wasn't fired either !
Wookai
What if you just hook it up to the OnClick event?
A: 

One of your controls in the hierarchy is missing an ID. Set the ID, and the event will fire. This also causes havoc with the AjaxControlToolkit (make sure every extender has an ID or clientside behaviors will often fail to load).

Richard Shadman