简体   繁体   English

如何在 asp.net 上的 bootstrap 5 中显示模态

[英]How to show modal in bootstrap 5 on asp.net

After upgrade to the Bootstrap 5 all of modal window not working.升级到 Bootstrap 5 后,所有模式窗口都不起作用。 Does anyone have any tips?有没有人有任何提示? I tried change namespace data-dismiss to data-bs-dismiss but still not working.我尝试将命名空间 data-dismiss 更改为 data-bs-dismiss 但仍然无法正常工作。 Thanks a lot.非常感谢。

protected void showmodal(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "myModal", "$('#myModal').modal({backdrop: 'static', keyboard: false});", true);
    upModal.Update();
}
<div class="d-grid gap-2 pb-2 px-2">
<asp:Button runat="server" ID="ShowModal" OnClick="showmodal" UseSubmitBehavior="false" Text="TEST MODAL" CssClass="btn" data-bs-toggle="modal" data-bs-target="#myModal" />
</div>

<!-- Bootstrap Modal Dialog -->
<div class="modal fade" id="myModal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
            <ContentTemplate>
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-bs-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title">
                            <asp:Label ID="Label5" runat="server" Text=""></asp:Label>
                        </h4>
                    </div>
                    <div class="modal-body">
                        <asp:Label ID="Label6" runat="server" Text=""></asp:Label>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-info" data-bs-dismiss="modal" aria-hidden="true">Close</button>
                    </div>
                </div>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
</div>

Assuming your modal has an id of "Modal1", I assume you used jQuery to display the modal like this:假设您的模态的 id 为“Modal1”,我假设您使用 jQuery 来显示模态,如下所示:

<script type="text/javascript">
    function openModal1() {
    $('#Modal1').modal('show');
    }
</script>

In C#, an asp.net button would call the javascript function like this:在 C# 中,一个 asp.net 按钮会像这样调用 javascript 函数:

protected void Button1_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "openModal1();", true);
    }

You just need a different javascript function:你只需要一个不同的 javascript 函数:

<script type="text/javascript">
    function openModal1() {
        var myModal = new bootstrap.Modal(document.getElementById('Modal1'), { keyboard: false });
        myModal.show();
    }
</script>

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

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