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

时间:2011-05-27 20:04:05

标签: javascript asp.net ajax postback modalpopupextender

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

这是我的代码:

JAVASCRIPT

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

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>

无论我在回发方法ShowGridPopup()之前还是之后调用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("_", "$"), '');
}

3 个答案:

答案 0 :(得分:9)

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

<强> .aspx的:

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

代码背后:

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

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

        mpeThePopup.Hide();
    }

答案 1 :(得分:1)

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

ModalPopupExtender

接下来,我们需要在使用保存事件

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

现在,在后面的代码中,我们需要做的就是检查// 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 } 文本字段的值,我们可以相应地在正确的弹出窗口中执行HiddenField

.Show()

答案 2 :(得分:1)

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