【问题标题】:JQuery dialog is not opening from C#JQuery 对话框未从 C# 打开
【发布时间】:2013-12-28 18:56:11
【问题描述】:

我在从 C# 代码隐藏打开 JQuery 对话框时遇到问题。它适用于我所做的其他页面,但在这个页面上它不起作用。 我已经尝试过thisthisthisthis。但我错过了一些东西。

基本上,左侧有一个带有 GridView 的表格,右侧有一个带有图像按钮的计数器。我要做的是向用户显示一个对话框,以确认他是否要在单击停止按钮 (btnZerar) 时重置计数器。

我在#StopDialogConfirm 上设置了一个断点,它仅在页面加载时到达,但在单击按钮时未到达。没有 javascript 错误。

按照我的代码示例:

ASPX:

<asp:Content ID="scriptsHeader" ContentPlaceHolderID="FeaturedContent" runat="server">
<script type="text/javascript" src="/Scripts/Pages/EstudeCiclos.js"></script>
<script type="text/javascript" src="/Scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="/Scripts/jquery-ui-1.8.20.min.js"></script>
</asp:Content>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">    
<asp:UpdatePanel ID="updPanelEstudeCiclos" UpdateMode="Conditional" runat="server"> <ContentTemplate>
...
                <div>
                    <asp:ImageButton ID="btnPlay" runat="server" OnClick="btnPlayClick" ImageUrl="~/Images/play.jpg" Height="40px" Width="40px" AlternateText="Começar a contar o tempo" />
                    <asp:ImageButton ID="btnPause" runat="server" OnClick="btnPauseClick" ImageUrl="~/Images/pause.jpg" Height="40px" Width="40px" AlternateText="Parar tempo" />
                    <asp:ImageButton ID="btnZerar" runat="server" OnClick="btnZerarClick" ImageUrl="~/Images/stop.jpg" Height="40px" Width="40px" AlternateText="Zerar tempo" />
                    <asp:ImageButton ID="btnSalvar" runat="server" OnClick="btnSalvarClick" ImageUrl="~/Images/save.jpg" Height="40px" Width="40px" AlternateText="Salvar tempo" />
                </div>
...
    <div>
        <asp:Button ID="btnStop" runat="server" OnClick="btnStop_Click" Style="display: none;" ClientIDMode="Static" />
    </div>
</ContentTemplate> </asp:UpdatePanel>

CodeBehind - 注释掉的代码是我尝试过的。当我尝试打开警报时,它会起作用。

    private void OpenQuestionDialog(string functionName, string question)
    {
        string s = "$(function(){$('#" + functionName + "').dialog('open').text('" + question + "');});";

        //ScriptManager.RegisterStartupScript(Page, this.GetType(), "Dialog", s, true);

        ScriptManager requestSM = ScriptManager.GetCurrent(this);
        if (requestSM != null && requestSM.IsInAsyncPostBack)
        {
            ScriptManager.RegisterClientScriptBlock(this,
                                                    typeof(Page),
                                                    Guid.NewGuid().ToString(),
                                                    s,
                                                    true);
        }
        else
        {
            ClientScript.RegisterClientScriptBlock(typeof(Page),
                                                   Guid.NewGuid().ToString(),
                                                   s,
                                                   true);
        }
    }


    protected void btnZerarClick(object sender, EventArgs e)
    {
        OpenQuestionDialog("StopDialogConfirm", "Are you sure you want to reset the counter?");
    }

脚本(EstudeCiclos.js):

$(function () {
$('#StopDialogConfirm').dialog({
    autoOpen: false,
    width: 450,
    modal: true,
    buttons: {
        "Não": function ()
        {
            $(this).dialog("close");
        },
        "Sim": function ()
        {
            $(this).dialog("close");
            $('#btnStop').click();
        }
    }
});
});

请帮忙!谢谢!

【问题讨论】:

  • 有jQuery冲突吗?
  • 您在 .ASP 的哪个位置创建了 #StopDialogConfirm 元素?
  • 您好,欢迎来到 Stack Overflow @Andre。您能否仅提供代码的相关部分以提供该问题的简短、独立的示例?
  • @adeneo 我没有在任何地方创建它,它只是在 javascript 文件中。
  • @Bagavatu 抱歉,我编辑了 aspx 代码,现在它变小了。

标签: c# javascript jquery asp.net dialog


【解决方案1】:

可能的问题是 RegisterClientScriptBlock 生成的脚本被添加到页面的开头,因此可能是没有加载或初始化适当的 jQuery 库。

有一些可能的尝试:

  1. 请改用RegisterStartupScript,因为它是在页面加载后执行的。如果在窗口 onload 方法中初始化 jquery,这仍然可能不起作用。

  2. 将要执行的方法添加到window.onload或内部pageLoad方法中,而不是直接在页面中。

  3. (我的偏好)将按钮执行代码更改为完全在客户端。如果您只是打开一个弹出窗口并且不需要页面中的任何内容,则没有理由执行完整的回发。

【讨论】:

  • 感谢您的回答.. 关于#1 不幸的是它不起作用.. 关于#2,您的意思是在 .js 文件中,该函数应该在 $(window).load(function () { $('#StopDialogConfirm').dialog ... } ? 关于#3,我可以用什么在客户端执行它?
  • @Andre:您可以将您的 javascript 直接放在页面内的函数中执行,然后更改按钮的代码以在 OnClientClick (msdn.microsoft.com/en-us/library/…) 中调用该函数。您需要从按钮中删除 OnClick 属性。
【解决方案2】:

这需要在 document.ready 函数中

function FlceSaveConfirmation() {
            $(function () {
                $("#dialog-flce").dialog({
                    resizable: false,
                    height: 200,
                    modal: true,
                    buttons: {
                        Ok: function () {

                            $("#tabs").tabs('option', 'enable', [4, 5, 6]);
                            FlceCeLevelInfo();
                            $(this).dialog("close");
                            //SelectParticularTab();
                        }
                    }
                });
            });
        }

在后面的代码中

Page . ClientScript . RegisterStartupScript ( this . GetType ( ) , "CallMyFunction" , "FlceSaveConfirmation()" , true );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多