A control with ID could not be found for the trigger in UpdatePanel

Dismissile picture Dismissile · Jul 12, 2011 · Viewed 33.1k times · Source

I have an update panel that has UpdateMode of Conditional and ChildrenAsTriggers set to false. I only want a few controls to cause an asynchronous postback:

<asp:UpdatePanel ID="updPnlMain" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>

      // ...
      <asp:Repeater ID="rptListData" runat="server">
          <ItemTemplate>
              <asp:Button ID="btnAddSomething" runat="server" OnClick="btnAddSomething_Click" />
          </ItemTemplate>
      </asp:Repeater>
      // ...
</ContentTemplate>
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="btnAddSomething" EventName="Click" />
</Triggers>
</asp:UpdatePanel>

I am getting the following error when I try and load this page:

A control with ID 'btnAddSomething' could not be found for the trigger in UpdatePanel 'updPnlMain'.

Since my btnAddSomething control is in a repeater and might not be there right away it acts like it is nonexistent. How can I get around this?

Answer

Muhammad Akhtar picture Muhammad Akhtar · Jul 12, 2011

Because your control is in the repeater control and it is out of scope to the Trigger collection. By the way you don't need to add trigger because your button control is already in the UpdatePanel, it will update when you click the button.

Edit: There is a solution if you really want to update your updPnlMain updatepanel. You can put in another updatepanel and put your button in that panel. e.g.

<asp:UpdatePanel ID="updButton" runat="server" UpdateMode="Conditional">
  <asp:Button ID="btnAddSomething" runat="server" OnClick="btnAddSomething_Click" />
</ContentTemplate>

and then simply call the updPnlMain.Update(); method in btnAddSomething_Click event.

It will actually do what you are looking for :)