Button inside Update Panel is not triggered, in asp.net

大兔子大兔子 提交于 2019-12-02 00:32:51

问题


<asp:ModalPopupExtender ID="MPE_EditGroup" runat="server" TargetControlID="btnShowPopup" PopupControlID="pnlpopup" CancelControlID="btnCancel" />
<asp:ToolkitScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
    <ContentTemplate>
        <asp:Panel ID="pnlpopup" runat="server">
            <asp:ListBox ID="lst_allmembers" DataValueField="FirstName" runat="server" />                       
            <asp:Button ID="btn_Add" runat="server" Text="Add" OnClick="btn_Add_Click" /><asp:ListBox ID="lst_grpmembers" runat="server" />
            <asp:Button ID="btn_remove" runat="server" Text="Remove" />
            <asp:Button ID="btnUpdate" CommandName="Update" runat="server" Text="Update" OnClick="btnUpdate_Click" />
            <asp:Button ID="btnCancel" runat="server" Text="Cancel" /></asp:Panel>
    </ContentTemplate>
</asp:UpdatePanel>

The add button has a event OnClick="btn_Add_Click"

  protected void btn_Add_Click(object sender, EventArgs e)
  {
        lst_grpmembers.Items.Add(lst_allmembers.SelectedItem.Text);
             }

The event is not triggered and when I click the add button nothing happens. And the Update Button was working fine before I added the update panel now only the cancel button closes the popup no other button works inside the pop up How to trigger the event.


回答1:


Change the UpdatePanel's ChildrenAsTriggers property to true. This will cause any postbacks triggered by the UpdatePanel's child elements to update its content.

EDIT: Just realized that btn_Add is a nested control, so you will have to explicitly call it out as an UpdatePanel Trigger. Add the following to your UpdatePanel markup, after the ContentTemplate:

<Triggers>
    <asp:AsyncPostBackTrigger ControlID="btn_Add" /> 
</Triggers>

EDIT #2: To keep your modal popup from closing when an async postback occurs, move the UpdatePanel inside the panel specified by ModalPopupExtender's PopupControlID:

<asp:Panel ID="pnlpopup" runat="server">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
        <ContentTemplate>
            <asp:ListBox ID="lst_allmembers" DataValueField="FirstName" runat="server" />
            <asp:Button ID="btn_Add" runat="server" Text="Add" OnClick="btn_Add_Click" />
            <asp:ListBox ID="lst_grpmembers" runat="server" />
            <asp:Button ID="btn_remove" runat="server" Text="Remove" />
            <asp:Button ID="btnUpdate" CommandName="Update" runat="server" Text="Update" OnClick="btnUpdate_Click" />
             <asp:Button ID="btnCancel" runat="server" Text="Cancel" />
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Panel>


来源:https://stackoverflow.com/questions/7650592/button-inside-update-panel-is-not-triggered-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!