Javascript 防止 ModalPopupExtender 在 PostBack 期间/之后关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6157168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:28:58  来源:igfitidea点击:

Preventing ModalPopupExtender from closing during/after PostBack

javascriptasp.netajaxpostbackmodalpopupextender

提问by JF Beaulieu

How do I prevent my asp:ModalPopupExtender from closing after or during a postback to the server??

如何防止我的 asp:ModalPopupExtender 在回发到服务器之后或期间关闭?

Here is my code:

这是我的代码:

JAVASCRIPT

爪哇脚本

// Confirm popup Ok button
function OnOk() {
    $('#confirmPopup').hide();
    ClickSaveButton();      // does a postback
    ShowGridPopup();
}

ASP.NET AJAX

ASP.NET AJAX

    <asp:ModalPopupExtender BehaviorID="confirmPopup" ID="confirmPopup" runat="server"
        TargetControlID="butConfirm" PopupControlID="ConfirmView" BackgroundCssClass="modalBackground"
        OnOkScript="OnOk();" OnCancelScript="OnCancel();"
        OkControlID="yesButton" CancelControlID="noButton">
    </asp:ModalPopupExtender>

No matter if I call ShowGridPopup()before or after the postback method ClickSaveButton(), the popup still dissapears. How can I prevent this?

无论我是ShowGridPopup()在 postback 方法之前还是之后调用ClickSaveButton(),弹出窗口仍然消失。我怎样才能防止这种情况?

EDIT

编辑

Here is the code for the ShowGridPopup()and ClickSaveButton()

下面是代码ShowGridPopup()ClickSaveButton()

function ShowGridPopup() {
    if (getID() == "Popup1") {
        ShowGridPopup1();
    } else if (getID() == "Popup2") {
        ShowGridPopup2();
    }
}

function ClickSaveButton() {
    var _id = $('a[id$="butSave"]').attr("ID");
    __doPostBack(_id.replace("_", "$"), '');
}

采纳答案by JF Beaulieu

I have found a way to by pass this, here is my solution: You have to create a new HiddenFieldcontroller from the server-side in ASP that will be used to store the ID of the ModalPopupExtenderthat you want to show after postback, it is set to null if there is no popup to be shown.

我找到了绕过这个的方法,这是我的解决方案:您必须HiddenField在 ASP 中从服务器端创建一个新控制器,该控制器将用于存储ModalPopupExtender您要在回发后显示的 ID ,它已设置如果没有要显示的弹出窗口,则为 null。

<!-- Grid Popup ID: to obtain the grid popup ID from server-side -->
<asp:HiddenField id="gridPopupID" runat="server" value="" />

Next, we need to set the ID to the HiddenFieldbefore we use the save event

接下来,我们需要HiddenField在使用save事件之前将ID设置为

// Confirm popup Ok button
function OnOk() {
    $('#confirmPopup').hide();                          // hides the current confirmation popup
    $("#<%= gridPopupID.ClientID %>").val(getID());     // set the ID to the hiddenField.
    ClickSaveButton();                                  // simulates the click of the save button
}

Now, in the code behind, all we need to do is check the value of the HiddenFieldtext field and we can just do a .Show()on the correct popup accordingly.

现在,在后面的代码中,我们需要做的就是检查HiddenField文本字段的值,我们可以.Show()相应地在正确的弹出窗口上做一个。

Protected Sub OnSaveAssociation(ByVal sender As Object, ByVal e As EventArgs) Handles butSaveAssociation.Click

' ommited code: save changes to back end

            ' determine which popup to show
            Dim _id As String = gridPopupID.Value
            Select Case _id
                Case "Popup1"
                    gridPopup1.Show()
                    gridPopupID.Value = Nothing
                Case "Popup2"
                    gridPopup2.Show()
                    gridPopupID.Value = Nothing
            End Select

End Sub

回答by mrbengi

You should use UpdatePanel, so the ModalPopupExtender won't be hidden after postback. See this example below :

您应该使用UpdatePanel,以便在回发后不会隐藏 ModalPopupExtender。请参阅下面的示例:

Aspx:

ASP:

<body> 
    <form id="form1" runat="server"> 
    <asp:ScriptManager ID="ScriptManager1" runat="server"> 
    </asp:ScriptManager> 
    <div> 
        <asp:UpdatePanel ID="udpOutterUpdatePanel" runat="server"> 
             <ContentTemplate> 

                 <asp:Button ID="ButtonShow" runat="server" Text="Show Popup" OnClick="ButtonShow_Click" />                 


                <input id="dummy" type="button" style="display: none" runat="server" /> 
                <ajaxToolkit:ModalPopupExtender runat="server" 
                        ID="mpeThePopup" 
                        TargetControlID="dummy" 
                        PopupControlID="pnlModalPopUpPanel" 
                        BackgroundCssClass="modalBackground"                        
                        DropShadow="true"/>
                 <asp:Panel ID="pnlModalPopUpPanel" runat="server" CssClass="modalPopup"> 
                    <asp:UpdatePanel ID="udpInnerUpdatePanel" runat="Server" UpdateMode="Conditional"> 
                        <ContentTemplate> 
                            Message
                            <asp:Button ID="ButtonClose" runat="server" Text="Close Popup" OnClick="ButtonClose_Click" />                                                                  
                        </ContentTemplate>       
                    </asp:UpdatePanel> 
                 </asp:Panel> 

            </ContentTemplate> 
        </asp:UpdatePanel> 
    </div> 
    </form> 
</body>

Code behind :

背后的代码:

    protected void ButtonShow_Click(object sender, EventArgs e)
    {
        //Show ModalPopup               

        mpeThePopup.Show(); 
    }
    protected void ButtonClose_Click(object sender, EventArgs e)
    {
        //Hide ModalPopup

        mpeThePopup.Hide();
    }

回答by ZahidKakar

Use, ModalPopupExtender1.Show() with update panel to avoid the whole page refresh. This has worked perfectly for me.

使用 ModalPopupExtender1.Show() 和更新面板来避免整个页面刷新。这对我来说非常有效。