views:

268

answers:

2

I've created a Footer UserControl that I dynamically add controls into based on a number of factors. I want some of those controls to be triggers for an UpdatePanel on the same page.

I have the following in code behind:

private void SetupFooter()
{

    WebToolBar footerToolBar = this.ShowFooterToolBar("~/ToolBars/PM_Footer.ascx");
    footerToolBar.ID = "PM_Footer";
    footerToolBar.OnNotify += new WebToolBar.OnNotifyHandler(footerToolBar_OnNotify);

    WebToolButton button = new WebToolButton();
    button.ID = "btn_Add";
    button.Text = "Add";
    button.ImageDefault = "~/Images/Icons/16/Plus.png";
    button.CommandName = "Add";
    button.Group = "1";
    footerToolBar.AddWebToolButton(button);


    AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
    trigger.ControlID = button.ID;
    trigger.EventName = "Click";

    pnl_Suppliers.Triggers.Add(trigger);

}

I get the error:

A control with ID 'btn_Add' could not be found for the trigger in UpdatePanel 'pnl_Suppliers'.

If I add the WebToolButton (basically an ImageButton with Text that I made) directly to the page and not inside the UserControl, then it works perfectly. Unfortunately I was hoping I could get this to work within my UserControl ("PM_Footer.ascx") as it encapsulates a lot of helper functionality and event hooks.

Is this at all possible? I'm clutching at straws and have tried assigning the UniqueID and ClientID to the update panel instead, but it just appears that any control within another user control cannot be used as a trigger for the UpdatePanel... which makes sense in a way.

+2  A: 

I do not know if what you ask for is possible, but why don't you raise a custom button click event via the WebToolBar control ? This event can then be directly assigned to the trigger.

Preets
+2  A: 

RegisterAsyncPostBackControl may work, but as Preeti stated, you might also have to expose an event from your control and register that event. Also, you are dynamically adding the control and it must re-added on each postback(OnPreRender) for the event to fire correctly.

ScriptManager1.RegisterAsyncPostBackControl(button );

Reference:

Dynamically Register an Asynchronous Postback Control with a ScriptManager

rick schott