LinkButton in nested UserControls does not fire Command event

I have a nested one UserControl

(this control is dynamically loaded by another UserControl

, which is dynamically loaded on an aspx page inside a MasterPage) in which I would like to use an event LinkButton

and OnCommand

.

This button needs to be added to some panel, so I connected to the OnLoad

panel event (it needs to be created before the 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 cannot get the Link_OnCommand method. One more thing that addresses my problem: UserControl is inside UpdatePanel. I also tried making the link a full postback trigger:

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

      

... but it doesn't change much, the page reloads completely, but no event is fired.

EDIT: As requested in the comments, here's more details on nesting:

MasterPage
  PlaceHolder
    Page
      UpdatePanel
        UserControl1
          FormView
            PlaceHolder
              UserControl2
                LinkButton

      

This means it is UserControl2

dynamically loaded.

+1


a source to share


3 answers


One of your controls in the hierarchy is missing an ID. Provide an identifier and the event will fire. This also wreaks havoc with the AjaxControlToolkit (make sure each extender has an ID or client behavior that won't load frequently).



+2


a source


I remember this problem too. I didn't find the real problem, but what I did was hook up an event handler in the aspx markup and it worked.

You can also try to hook up an event handler earlier in the page's lifecycle.



Try the PreInit event handler.

More information on the page lifecycle here .

0


a source


for ajax i think you can create a reusable class

public void registerUCAsyncPostBack(Page page, WebControl webcontrol, UserControl usercontrol){
            Control ControlAjaxNew = null;
            if (webcontrol.GetType() == typeof(LinkButton))
            {
                ControlAjaxNew = (LinkButton)usercontrol.FindControl(webcontrol.ID);
            }
            ScriptManager.GetCurrent(page).RegisterAsyncPostBackControl(ControlAjaxNew);
        }

      

0


a source







All Articles