6

我在我的 c# asp.net 项目中使用引导程序,我想显示从后面的代码中显示模式弹出窗口。

在我的页眉中,我有一个 javascript 函数,如下所示:

function LoginFail() {
        $('#windowTitleDialog').modal('show');
    }

并单击我的按钮,我正在调用 javascript,如下所示

ScriptManager.RegisterClientScriptBlock(this, typeof(System.Web.UI.Page), "LoginFail", "LoginFail();", true);

这不显示模式弹出窗口。但是,如果我使用类似的东西alert('login failed'),它工作正常。

有人可以帮忙吗?

4

5 回答 5

14

默认情况下,Bootstrap javascript 文件包含在结束 body 标记之前

    <script src="vendors/jquery-1.9.1.min.js"></script>
    <script src="bootstrap/js/bootstrap.min.js"></script>
    <script src="vendors/easypiechart/jquery.easy-pie-chart.js"></script>
    <script src="assets/scripts.js"></script>
 </body>

我将这些 javascript 文件放入 body 标记之前的 head 部分,并编写了一个小函数来调用模态弹出窗口:

<script src="vendors/jquery-1.9.1.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<script src="vendors/easypiechart/jquery.easy-pie-chart.js"></script>
<script src="assets/scripts.js"></script>

<script type="text/javascript">
function openModal() {
    $('#myModal').modal('show');
}
</script>
</head>
<body>

然后我可以使用以下代码从代码隐藏中调用模式弹出窗口:

protected void lbEdit_Click(object sender, EventArgs e) {   
  ScriptManager.RegisterStartupScript(this,this.GetType(),"Pop", "openModal();", true);
}
于 2013-09-22T18:08:41.297 回答
5

我就是这样解决的:

脚本功能

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

按钮声明

<asp:Button ID="LaunchModal" runat="server" Text="Launch Modal" CssClass="btn" onclick="LaunchModal_Click"/>

请注意,未设置属性 data-togle="modal"。

LaunchModal_Click 事件中的服务器端代码为:

ScriptManager.RegisterStartupScript(this, this.GetType(), "LaunchServerSide", "$(function() { LoginFail(); });", true);

我希望这对你有帮助。

于 2013-04-09T06:38:44.647 回答
0

我假设,您正在使用 jQuery 模态插件之一。在这种情况下,我怀疑插件代码在调用时没有初始化(这将在它呈现到页面之后立即进行)。在执行插件代码后,您必须推迟对该函数的调用。您的案例的实施可能如下所示:

ScriptManager.RegisterClientScriptBlock(this, typeof(System.Web.UI.Page), "LoginFail", "$(function() { LoginFail(); });", true);
于 2013-03-28T17:47:22.483 回答
0

我也遇到了同样的问题,想找网上的解决办法,但是没办法。正如您从后面的代码中所说的那样,我可以调用其他 javascript 函数,但是当我添加模态 js 函数时,模态不会出现。

我的猜测是 modal.('show') 函数没有被调用,因为当我在 crhome 中检查 modal 元素时,该类是 modal hide fade 而不是 modal hide fade IN,其他属性保持不变。

一个可能的解决方案是从后面的代码中更改这些属性,我的意思是你可以从后面的代码中做 js 所做的事情,可能不是最好的解决方案,但我没有别的办法。通过这样做,我让模态显示为背景灰色,但无法使效果起作用。

于 2013-04-04T14:41:28.817 回答
0

我在 aspx 页面中执行此操作:

<asp:Panel ID="MessagePanel" runat="server" CssClass="hide"  EnableViewState="false">
<div class="modal-header">
<asp:Button type="button" ID="btnClose" runat="server" data-dismiss="modal" Text="x" />
</div>
<div class="modal-body">
<h4>What's new in this version</h4>
... rest of bootstrap modal stuff....
</div>
</asp:Panel>

然后在代码隐藏中显示任何事件(例如 page.load 或按钮单击)的模式弹出窗口,我只需更改面板的 CssClass:

MessagePanel.CssClass = "modal fade in"

不需要 js 或 ScriptManager。

于 2015-04-15T12:55:24.513 回答