简体   繁体   English

防止在 ASP.NET 回发时关闭引导模式

[英]Prevent bootstrap modal closing on postback in ASP.NET

I have a bootstrap modal I want to show in order to perform a search and select funcionality in a ASP.NET Web form.我有一个我想显示的引导模式,以便在 ASP.NET Web 表单中执行搜索和 select 功能。 The problem is when the user clicks on "Search" button, the modal closes.问题是当用户单击“搜索”按钮时,模式关闭。 I want the modal keeps open to show search results on GridView below and closes when the user selects any GridView item.我希望模式保持打开状态以在下面的 GridView 上显示搜索结果,并在用户选择任何 GridView 项目时关闭。

I've tried other solutions mentioned in other threads but nothing seems to work.我尝试了其他线程中提到的其他解决方案,但似乎没有任何效果。 I'm using a WebForm with a Master page, I don't know if it can be causin me the issue.我正在使用带有母版页的 WebForm,我不知道它是否会引起我的问题。

This is my modal code:这是我的模态代码:

<div class="modal fade" id="modSearchByAccount" role="dialog">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Search by Account</h4>
        </div>
        <div class="modal-body">
            <div class="row">
                <div class="col-md-8">
                    <asp:TextBox ID="txtSearchText" runat="server" CssClass="form-control" placeholder="Account name"></asp:TextBox>
                </div>
                <div class="col-md-4">
                     <asp:Button ID="btnSearchAccount" runat="server" OnClick="btnSearchAccount_Click" Text="Buscar" CssClass="btn btn-default" />
                </div>    
            </div>
            <div class="row">
                <div class="col-md-12">
                     <asp:Panel ID="pnlSearchResults" runat="server" ScrollBars="Vertical" Height="200px" Width="100%">
                        <asp:GridView ID="gvSearchResults" runat="server" AutoGenerateColumns="False" DataKeyNames="ACCOUNT_ID" OnSelectedIndexChanged="gvSearchResults_SelectedIndexChanged" CssClass="table table-striped table-hover">
                            <Columns>
                                <asp:BoundField DataField="ACCOUNT_ID" HeaderText="ID" />
                                <asp:BoundField DataField="ACCOUNT_NAME" HeaderText="Name" />
                                <asp:CommandField ShowSelectButton ="true" />
                            </Columns>
                        </asp:GridView>
                    </asp:Panel>
                </div>
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
    </div> 
 </div>

Any ideas?有任何想法吗? Can I use an UpdatePanel somewhere here?我可以在这里的某个地方使用 UpdatePanel 吗?

Thanks in advance!提前致谢!

Probably too late now, but yes it can be done.现在可能为时已晚,但是是可以做到的。 The key is to have an outer UpdatePanel and an inner UpdatePanel.关键是要有一个外部 UpdatePanel 和一个内部 UpdatePanel。 The outer panel should be set to an UpdateMode of conditional and ChildrenAsTriggers = true.外部面板应设置为条件和 ChildrenAsTriggers = true 的 UpdateMode。

In my case I moved the inner body into a User Control, but your code example should work as follows:在我的情况下,我将内部主体移动到用户控件中,但您的代码示例应如下工作:

<asp:UpdatePanel runat="server" ID="updatePanelTop" UpdateMode="Conditional" ChildrenAsTriggers="True">
<ContentTemplate>
    <div class="modal fade" id="modSearchByAccount" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Search by Account</h4>
            </div>
            <div class="modal-body">
                <asp:UpdatePanel runat="server">
                <ContentTemplate>
                    <div class="row">
                        <div class="col-md-8">
                            <asp:TextBox ID="txtSearchText" runat="server" CssClass="form-control" placeholder="Account name"></asp:TextBox>
                        </div>
                        <div class="col-md-4">
                             <asp:Button ID="btnSearchAccount" runat="server" OnClick="btnSearchAccount_Click" Text="Buscar" CssClass="btn btn-default" />
                        </div>    
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                             <asp:Panel ID="pnlSearchResults" runat="server" ScrollBars="Vertical" Height="200px" Width="100%">
                                <asp:GridView ID="gvSearchResults" runat="server" AutoGenerateColumns="False" DataKeyNames="ACCOUNT_ID" OnSelectedIndexChanged="gvSearchResults_SelectedIndexChanged" CssClass="table table-striped table-hover">
                                    <Columns>
                                        <asp:BoundField DataField="ACCOUNT_ID" HeaderText="ID" />
                                        <asp:BoundField DataField="ACCOUNT_NAME" HeaderText="Name" />
                                        <asp:CommandField ShowSelectButton ="true" />
                                    </Columns>
                                </asp:GridView>
                            </asp:Panel>
                        </div>
                    </div>
                </ContentTemplate>
                </asp:UpdatePanel>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div> 
     </div>
</ContentTemplate>
</asp:UpdatePanel>     

If you are using the UpdatePanel with Select2List or Some other bootstrap like FileUpload Controls, they will not properly render on Auto Post Back如果您将 UpdatePanel 与 Select2List 或其他一些引导程序(如 FileUpload Controls)一起使用,它们将无法在Auto Post Back上正确呈现

To avoid this don't use the Update Panel just add the following code in code behind.为避免这种情况,请不要使用更新面板,只需在后面的代码中添加以下代码即可。 This code will keep your Bootstrap Popup open after AutoPostBack Event.此代码将使您的 Bootstrap 弹出窗口在 AutoPostBack 事件后保持打开状态。

 protected void OnSelectedIndexChanged(object sender, EventArgs e) { ClientScript.RegisterStartupScript(this.GetType(), "Popup", "$('#MyPopup').modal('show')", true); }

I know this is old, but I came upon this thread with the same problem.我知道这很旧,但我遇到了同样的问题。 The other solutions did not work for me, but I came up with my own that seems to work, although it might be considered a "hack".其他解决方案对我不起作用,但我想出了我自己的似乎有效的解决方案,尽管它可能被认为是“黑客”。 Basically, in the code-behind I set up the various attributes of HTML elements the same way that Bootstrap does it.基本上,在代码隐藏中,我以与 Bootstrap 相同的方式设置 HTML 元素的各种属性。 I figured out what to set them to by comparing the Page Source when the dialog was "closed" vs. when "open".我通过比较对话框“关闭”和“打开”时的页面源,找出了将它们设置为什么。

Here's the top of the markup for my modal:这是我的模态标记的顶部:

                        <div id="modalSizeChange" runat="server" class="modal hide fade" tabindex="-1" role="dialog" aria-hidden="true">
                        <div class="modal-header">
                            <asp:Button ID="btnCloseMatItem" class="close" runat="server" Text='&times;' OnClick="btnCloseMatItem_Click" OnClientClick="$('#modalSizeChange').modal('hide')" />
                            <h2 id="ResizeDlgHdr" runat="server">Change Size of Return Piece</h2>
                        </div>

And here are two subroutines (this is vb.net) for keeping the modal open or closing it:这里有两个子程序(这是 vb.net)用于保持模式打开或关闭它:

    Private Sub HideResizeDlg()
    modalSizeChange.Style.Remove("display")
    modalSizeChange.Attributes("class") = "modal hide fade"
    modalSizeChange.Attributes("aria-hidden") = "true"
    litDlgBackdrop.Text = ""
    litDlgBackdrop.Visible = False

    'Following is to remove any Div with "modal-backdrop fade in" class created by JQuery (instead of the one created by code-behind)
    Dim script As String = "$('.modal-backdrop').remove();"
    ScriptManager.RegisterStartupScript(Me, Me.GetType(), "#modalBackdropRemove", script, True)
End Sub

Private Sub ShowResizeDlg()
    'Set the attributes of the dialog so it stays visible on the post back - this matches the state after the JQuery .modal('show') is called
    modalSizeChange.Style.Add("display", "block")
    modalSizeChange.Attributes("class") = "modal hide fade in"
    modalSizeChange.Attributes("aria-hidden") = "false"
    litDlgBackdrop.Text = "<div class=""modal-backdrop fade in""></div>"
    litDlgBackdrop.Visible = True

End Sub

So whenever during postback I want to keep the dialog displayed, I call ShowResizeDlg().因此,每当在回发期间我想保持对话框显示时,我都会调用 ShowResizeDlg()。 The one problem with this approach is that after reloading the page, the close/cancel buttons no longer work using basic client-side .modal('hide') call, for the same reasons that the dialog didn't stay open.这种方法的一个问题是,在重新加载页面后,关闭/取消按钮不再使用基本的客户端 .modal('hide') 调用工作,原因与对话框没有保持打开状态相同。 So to get around that I made those post-back buttons as well, and they call the HideResizeDlg() subroutine which does the equivalent of 'hide'.因此,为了解决这个问题,我也制作了那些后退按钮,它们调用了 HideResizeDlg() 子例程,该子例程相当于“隐藏”。

A note about litDlgBackdrop, which is a literal at the bottom just before the </body> tag.关于 litDlgBackdrop 的注释,它是位于</body>标记之前底部的文字。 I found out that the version of bootstrap used in my app achieves the darkened background effect by adding a <div> at this location with the class="modal-backdrop fade in".我发现我的应用程序中使用的 bootstrap 版本通过在这个位置添加一个<div>和 class="modal-backdrop fade in" 来实现变暗的背景效果。 So to achieve the same affect I populate this literal with the same thing.所以为了达到同样的效果,我用同样的东西填充这个文字。

In the HideResizeDlg(), I register a startup script that looks for and deletes any elements with class "modal-backdrop" - to get rid of the one created by bootstrap.在 HideResizeDlg() 中,我注册了一个启动脚本,该脚本查找和删除具有“modal-backdrop”类的任何元素 - 以摆脱引导程序创建的元素。

One other caution, the app I'm working on is using an old Bootstrap version from 2013, so the attributes I'm setting may not work with newer versions, but you could take a similar approach by studying the Page Source like I did to find out what to change.另一个警告,我正在开发的应用程序使用的是 2013 年的旧 Bootstrap 版本,因此我设置的属性可能不适用于较新版本,但您可以通过研究页面源来采取类似的方法,就像我所做的那样找出要更改的内容。

There's probably a much better way, I just found this works for my purposes.可能有更好的方法,我只是发现这适合我的目的。

Put your Modal outside the updatepanel, if the modal inside the updatepanel the modal will close automatically when postback or asp button clicked.将您的模态框放在更新面板之外,如果模态框位于更新面板内,则在单击回发或 asp 按钮时模态框将自动关闭。

<div class="modal fade" id="mdlGuncelle" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-keyboard="false" data-backdrop="static">
<asp:UpdatePanel ID="upModalGuncelle" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
    <ContentTemplate>
       
            <div class="modal-dialog">

Modal popup özelliğini Update Panel dışına alın.模态弹出 özelliğini 更新面板 dışına alın。 PostBack olacak kısımları Update panel içerisine koyması işinizi görecektir. PostBack olacak kısımları 更新面板 içerisine koyması işinizi görecektir。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM