关闭ajaxToolkit:ModalPopupExtender时出错

前端开发 2026-07-09

我有一个网页。

我展示一个ajaxToolkit:ModalPopupExtender(MPE_Example_Panel_Like_Browser)。

我点击了ajaxToolkit:ModalPopupExtender(MPE_Example_Panel_Like_Browser) 上的按钮,然后显示另一个ajaxToolkit:ModalPopupExtender(MPE_Example_Panel_Like_Browser_Confirmation)。

当我点击第二个ajaxToolkit:ModalPopupExtender(MPE_Example_Panel_Like_Browser_Confirmation) 上的OK按钮时,我执行了点击事件。

这随后会隐藏第二个ajaxToolkit:ModalPopupExtender(MPE_Example_Panel_Like_Browser_Confirmation),并把一些文本赋给某些标签。

问题在于,第一个ajaxToolkit:ModalPopupExtender(MPE_Example_Panel_Like_Browser) 会关闭。

我只想让第二个ajaxToolkit:ModalPopupExtender(MPE_Example_Panel_Like_Browser_Confirmation) 关闭。

Markup:-

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.4000.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Tests</title>
<link href="../Style_Sheet/Stylesheet1.css" rel="stylesheet" type="text/css" />
<style>
</style>
<script>
</script>

</head>
<body>

    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <div>
        <asp:Label ID="lbModal_Response_Display" runat="server" Text=""></asp:Label>

        <!-- ModalPopupExtender Panel Like Browser -->

        <asp:Button ID="btnMPE_Example_Panel_Like_Browser" runat="server" Text="Display Panel Like Browser In A Modal Popup Extender"  />        
        <!-- Modal Popup Panel -->
        <asp:Panel ID="pnlMPE_Example_Panel_Like_Browser" style="border: 2px solid #ccc; width: 780px; height:950px;" runat="server" BackColor="#666666">    
            <!-- Title Bar & Controls -->
            <asp:Panel ID="pnlMPE_Example_Panel_Like_Browser_Header" runat="server" >
                <div > 
                </div>
            </asp:Panel>

            <!-- Main Content -->
            <asp:Panel ID="pnlMPE_Example_Panel_Like_Browser_Content" runat="server" style="height:945px;" BackColor="White">
                <h3>We are restricted to the Browser Height/Width</h3>
                <p>This panel acts as an inner browser window.</p>

                <asp:Label ID="lbMPE_Example_Panel_Like_Browser_Saved" runat="server" Text=""></asp:Label>
                <asp:Button ID="btMPE_Example_Panel_Like_Browser_Save" runat="server" Text="Save" CausesValidation = "false" onclick="btnMPE_Example_Panel_Like_Browser_Save_Click"/>
                <asp:Button ID="btnMPE_Example_Panel_Like_Browser_Close" runat="server" Text="Close" onclick="btnMPE_Example_Panel_Like_Browser_Close_Click" CausesValidation = "false"/>
                <asp:Button ID="btnMPE_Example_Panel_Like_Browser_Message" runat="server" Text="Message" CausesValidation="false" onclick="btnMPE_Example_Panel_Like_Browser_Message_Click"/>

            </asp:Panel>    
        </asp:Panel>


        <!-- ModalPopupExtender Control -->
        <ajaxToolkit:ModalPopupExtender ID="MPE_Example_Panel_Like_Browser" runat="server"
            TargetControlID="btnMPE_Example_Panel_Like_Browser"
            PopupControlID="pnlMPE_Example_Panel_Like_Browser"
            BackgroundCssClass="modalBackground"
            BehaviorID="mpe_bhid2">
        </ajaxToolkit:ModalPopupExtender>

        <!-- ModalPopupExtender Panel Like Browser Confirmation -->
        <!-- Modal Popup Panel -->
        <asp:Panel ID="pnlMPE_Example_Panel_Like_Browser_Confirmation" runat="server" CssClass="modalPopup" >
            <h3>Modal Popup</h3>
            <p>Are you sure you want to Save?</p>
            <asp:Button ID="btnMPE_Example_Panel_Like_Browser_Confirmation_OK" runat="server" Text="OK" OnClick="btnMPE_Example_Panel_Like_Browser_Confirmation_OK_Click" CausesValidation="false"/>
            <asp:Button ID="btnMPE_Example_Panel_Like_Browser_Confirmation_Cancel" runat="server" Text="Cancel" OnClick="btnMPE_Example_Panel_Like_Browser_Confirmation_Cancel_Click" CausesValidation="false"/>
        </asp:Panel>
        <!-- ModalPopupExtender Control -->
        <ajaxToolkit:ModalPopupExtender ID="MPE_Example_Panel_Like_Browser_Confirmation" runat="server"
            TargetControlID="btMPE_Example_Panel_Like_Browser_Save"
            PopupControlID="pnlMPE_Example_Panel_Like_Browser_Confirmation"
            BackgroundCssClass="modalBackground"
            BehaviorID="mpe_bhid3">
        </ajaxToolkit:ModalPopupExtender>
        <!-- ModalPopupExtender Panel Like Browser Confirmation -->

        <!-- ModalPopupExtender_Panel Like Browser -->

    </form>
</body>

</html>

Code Behind:-

using AjaxControlToolkit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {

        #region Modal Popup Example Panel Like Browser.
        protected void btnMPE_Example_Panel_Like_Browser_Close_Click(object sender, EventArgs e)
        {
            // DONT SET THE CANCELCONTROLID on the ModalPopupExtender
            // Set the CausesValidation = false on btnModal_Popup_No 
            // for this event to be fired
            MPE_Example_Panel_Like_Browser.Hide();
            lbModal_Response_Display.Text = "Modal Popup Example Panel Like Browser - Close";
        }

        protected void btnMPE_Example_Panel_Like_Browser_Save_Click(object sender, EventArgs e)
        {
            lbMPE_Example_Panel_Like_Browser_Saved.Text = "Save";           
            MPE_Example_Panel_Like_Browser_Confirmation.Show();            
        }
        #endregion Modal Popup Example Panel Like Browser.

        #region Modal Popup Example Panel Like Browser Confirmation.
        protected void btnMPE_Example_Panel_Like_Browser_Confirmation_OK_Click(object sender, EventArgs e)
        {
            MPE_Example_Panel_Like_Browser_Confirmation.Hide();            
            lbMPE_Example_Panel_Like_Browser_Saved.Text = "Saved";            
            lbModal_Response_Display.Text = "Modal Popup Example Panel Like Browser Confirmation - OK";
        }
        protected void btnMPE_Example_Panel_Like_Browser_Confirmation_Cancel_Click(object sender, EventArgs e)
        {
            MPE_Example_Panel_Like_Browser_Confirmation.Hide();            
            lbMPE_Example_Panel_Like_Browser_Saved.Text = "Cancel";            
            lbModal_Response_Display.Text = "Modal Popup Example Panel Like Browser Confirmation - Cancel";
        }
        #endregion Modal Popup Example Panel Like Browser Confirmation.

    }
}

CSS:-

.modalPopup {
    background-color: #fff;
    border: 3px solid #ccc;
    padding: 10px;
    width: 300px;
}

Solution as per WebSurfer reply. Need to reshow the MPE_Example_Panel_Like_Browser:-

        #region Modal Popup Example Panel Like Browser Confirmation.
    protected void btnMPE_Example_Panel_Like_Browser_Confirmation_OK_Click(object sender, EventArgs e)
    {
        MPE_Example_Panel_Like_Browser_Confirmation.Hide();            
        lbMPE_Example_Panel_Like_Browser_Saved.Text = "Saved";            
        lbModal_Response_Display.Text = "Modal Popup Example Panel Like Browser Confirmation - OK";
        MPE_Example_Panel_Like_Browser.Show();
    }
    protected void btnMPE_Example_Panel_Like_Browser_Confirmation_Cancel_Click(object sender, EventArgs e)
    {
        MPE_Example_Panel_Like_Browser_Confirmation.Hide();            
        lbMPE_Example_Panel_Like_Browser_Saved.Text = "Cancel";            
        lbModal_Response_Display.Text = "Modal Popup Example Panel Like Browser Confirmation - Cancel";
        MPE_Example_Panel_Like_Browser.Show();
    }
    #endregion Modal Popup Example Panel Like Browser Confirmation.

解决方案

我理解,你的问题基本上与这个问题以及你另一个问题 Problem displaying a message on a ajaxToolkit:ModalPopupExtender 相同,即当你关闭信息时,主ModalPopup也会被关闭。区别只是本问题使用ModalPopup来显示信息,而另一问题使用JavaScript警报。

正如我在回答你另一个问题时所提到的,你必须通过编程方式显示ModalPopup。

请注意,点击ASP.NET Web Forms的 Button控件会导致回发(postback),从而提交表单。因此,浏览器向服务器发送请求,服务器向浏览器返回响应,浏览器使用服务器返回的响应重新绘制页面,除非你在该页面上再次显示它,否则ModalPopup不会显示。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章