C# – Trigger UpdatePanel from Dropdownlist in ChildControl

ajaxasp.netcdrop-down-menuupdatepanel

This is my situation:

Page:

<asp:UpdatePanel ID="updatePanel1" runat="server" ChildrenAsTriggers="true">
  <ContentTemplate>
    ...
    <uc:ChildControl ID="ucChild" runat="server" />
    ...
  </ContentTemplate>
</asp:UpdatePanel>

ChildControl:

...
<asp:DropDownList id="dropDown1" runat="server" />
...

I want to update the UpdatePanel (asynchronously) in the Page when the selection of the DropDownList in the ChildControl changes. I tried AutoPostBack="true", but this always leads to a full PostBack of the Page (see this question).

I tried to use

<Triggers>
    <asp:AsyncPostBackTrigger ControlID="???" EventName="SelectedIndexChanged" />
</Triggers>

but neither "dropDown1" nor "ucChild.dropDown1" worked as values for ControlID.

I also tried to pass a reference of the UpdatePanel to the ChildControl and add a Trigger in the following way:

protected override void OnPreRender(EventArgs e)
{
    if (ParentUpdatePanel != null)
    {
        AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
        trigger.ControlID = dropDown1.ID;
        trigger.EventName = "SelectedIndexChanged";
        ParentUpdatePanel.Triggers.Add(trigger);
    }
    base.OnPreRender(e);
}

(also tried with dropDown1.ChildID)

But I am still unable to get the UpdatePanel to trigger when the value in the Dropdown changes. The problem seems to be that the UpdatePanel cannot see the Control in the ChildControl and therefore is unable to set the Trigger accordingly.

How can I do it?

Best Answer

Maybe with a trick putting this code on dropdown list.

dropDown1.Attributes["onchange"] =   
Page.ClientScript.GetPostBackEventReference(ParentUpdatePanel, "") + "; return false;";

When you change the drop down list you send an update event to UpdatePanel using direct javascript call.