Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...