jQuery对话框在屏幕上闪烁,而不是留在用户输入

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jQuery对话框在屏幕上闪烁,而不是留在用户输入相关的知识,希望对你有一定的参考价值。

在这个Asp.net webform应用程序中,我不得不添加一个jQuery对话框,因为客户端需要一个自定义弹出确认框。我不得不在后面的代码中附加对话框,因为对话框只会添加到GridView的某些行中的按钮中。当单击按钮以显示对话框时,它仅在屏幕上闪烁,没有时间让用户单击按钮。以为是GridView_RowCommand事件结束时的重定向引起了这种情况,但一旦被注释掉,它仍然在屏幕上闪现。认为它与按钮本身的回发功能有关,但似乎无法找到它。尝试通过jQuery而不是后面的代码中的OnClientClick属性附加对话框的开头,但这似乎也没有用。有人可以看看代码片段,看看我做错了什么或建议另一种方法来获得我需要做的事情?这是阻止我完成这个项目的唯一障碍。非常感谢你的帮助。

对话框的html

<div style="display:none" id="dialog-warning" title="Out of Network">
        <p class="CallOut">
            <asp:Label ID="lblOutOfNetworkMsg" HtmlEncode="false" runat="server"></asp:Label>
        </p>
        <p style="text-align:center;">
        <asp:Button style="width:200px; background-color:#E9967A; font-family:Arial; font-size:11px; font-weight:bold; border:2px solid black;" ID="btnBack" runat="server" OnClientClick="return true;" CausesValidation="false" /><br /><br />
        <asp:Button style="width:200px; background-color:#E9967A; font-family:Arial; font-size:11px; font-weight:bold; border:2px solid black;" ID="btnProceed" runat="server" OnClientClick="return false;" CausesValidation="false" />
        </p>
    </div>

用于样式化对话框,打开对话框并将其附加到所需按钮的jQuery:

$(function () {
            var dlg = $("#dialog-warning").dialog({
                resizable: false,
                height: 200,
                width: 300,
                modal: true,
                autoOpen: false,
                open: function (event, ui) {
                    $(".ui-dialog-titlebar-close", ui.dialog).hide();
                }
            }).prev(".ui-dialog-titlebar").css("background", "#A0A0A0").css("font-family", "Arial").css("font-weight", "bold").css("color", "white").css("text-align", "center").css("font-size", "11px");
            dlg.parent().appendTo(jQuery("form:first"));
        });

        function OpenDialog() {
            $("#dialog-warning").dialog("open");
        }

        $(document).ready(function () {
            $('[outofnetwork=true]').click(function () {
                OpenDialog();
            })
        })

GridView_RowDataBound事件(添加属性以标识哪些行需要附加对话框,然后在$(document).ready()中使用jQuery将函数附加到打开对话框):

else
                    {
                        e.Row.Cells[11].Style.Add("color", "#ff0000");

                        if (string.IsNullOrEmpty(ss.PhysicalServices))
                        {
                            //btn.OnClientClick = "OpenDialog();";
                            btn.Attributes.Add("outofnetwork", "true");
                        }
                    }

GridView_RowCommand事件(单击按钮后执行的代码):

if (e.CommandName == "Navigate")
            {
                //CommandArgument comes in as XXX|YYY|ZZZ where XXX is the SiteID and YYY is the Network
                //and ZZZ is the NetworkSort value. We parse out the values by splitting the CommandArgument on the pipe symbol
                //Element 0 (zero) is the siteid and element 1 (one) is the network and element 2 (two) is the NetworkSort value.

                oEventFlow.ClinicDetails.SiteID = Convert.ToInt32(e.CommandArgument.ToString().Split('|')[0].ToString());
                oEventFlow.ClinicDetails.Network = Convert.ToInt32(e.CommandArgument.ToString().Split('|')[1].ToString());
                oEventFlow.ClinicDetails.NetworkSort = Convert.ToInt32(e.CommandArgument.ToString().Split('|')[2].ToString());

                Session[EventFlow.cEVENTFLOWSessionKey] = oEventFlow;

                //Redir(oEventFlow.SiteSelectionRedirect);

                //int selectedIndex = Convert.ToInt32(e.CommandArgument.ToString().Split('|')[3].ToString());
                //GridViewRow selectedRow = gridResults.Rows[selectedIndex];

                //if (selectedRow.Cells[11].Text != "OUT OF NETWORK")
                //{
                //    Redir(oEventFlow.SiteSelectionRedirect);
                //}
            }

希望这清楚我想要完成的事情。如果没有,请评论,我会纠正。再次感谢。

答案

我不得不使用一些小技巧来解决这个问题,使用jQuery,另一个服务器控件按钮和几个隐藏字段。

GridView_RowDataBound事件处理程序中的代码,用于将类添加到符合条件的asp按钮。

                    if ((PhysicalNetworkTypes)ss.PhysicalNetworkType == PhysicalNetworkTypes.ContractedInNetwork)
                    {
                        e.Row.Cells[11].Style.Add("color", "#63d834");
                    }
                    else
                    {
                        e.Row.Cells[11].Style.Add("color", "#ff0000");

                        if (string.IsNullOrEmpty(ss.PhysicalServices))
                        {
                            btn2.CssClass += " outofnetwork";
                        }
                    }

我添加了一个asp:Button到与原始OneClickButton相同的列,我最终隐藏了。新按钮将处理确定对话框何时显示的所有前端功能。

                            <ItemTemplate>
                                <cc2:OneClickButton Text='Select' CssClass='NavButton' runat='server' ID='btnGetReport' CausesValidation='false'
                                    CommandName='Navigate' Width="100" style="display:none;" />
                                <asp:Button Text="Select" CssClass="NavButton network" runat="server" ID="btnDummy" CausesValidation="false" CommandName="Navigate" Width="100" OnClientClick="return false;" />
                            </ItemTemplate>

我添加了一个隐藏字段,该字段将保存OneClickButton的相应名称,另一个隐藏字段将保持值为true,如果单击对话框中的“继续下一步”按钮。对话框中的“返回搜索诊所”按钮将关闭对话框。对话框中的“继续下一步”按钮将调用其在btnToClick隐藏字段中保存的相应OneClickButton的Click事件。

<input type="hidden" id="btnProceedClicked" runat="server" />


<div style="display:none" id="divWarning" title="Out of Network">
    <p class="CallOut">
        <asp:Label ID="lblOutOfNetworkMsg" HtmlEncode="false" runat="server"></asp:Label>
    </p>
    <p style="text-align:center;">
    <asp:Button style="width:200px; background-color:#E9967A; font-family:Arial; font-size:11px; font-weight:bold; border:2px solid black;" ID="btnBack" runat="server" OnClientClick="$('#divWarning').dialog('close');" CausesValidation="false" /><br /><br />
    <Button style="width:200px; background-color:#E9967A; font-family:Arial; font-size:11px; font-weight:bold; border:2px solid black;" ID="btnProceed" CausesValidation="false">Proceed to Next Step</Button><input type="hidden" id="btnToClick" />
    </p>
</div>

在$(document).ready()中,我通过替换Asp.net Button的名称并将该值放在btnToClick隐藏字段中来获取OneClickButton的ID。该对话框仅针对具有一类outofnetwork的asp按钮显示,该类在后面的代码中的GridView_RowDataBound事件处理程序中设置。否则,将为相应的OneClickButton调用click()方法。此外,如果在对话框中单击了“继续下一步”按钮,则会将值true存储在btnProceedClicked隐藏字段中。在GridView_RowCommand事件处理程序中使用此值来确定是否应该重定向。

$(function () {
            var dlg = $("#divWarning").dialog({
                resizable: false,
                height: 200,
                width: 300,
                modal: true,
                autoOpen: false,
                open: function (event, ui) {
                    $(".ui-dialog-titlebar-close", ui.dialog).hide();
                }
            }).prev(".ui-dialog-titlebar")
                .css("background", "#A0A0A0")
                .css("font-family", "Arial")
                .css("font-weight", "bold")
                .css("color", "white")
                .css("text-align", "center")
                .css("font-size", "11px");
            dlg.parent().appendTo(jQuery("form:first"));
        });

        $(document).ready(function () {
            $('.network').click(function () {
                console.log(this);

                var btnID = '#' + $(this).attr("id").replace("btnDummy", "btnGetReport");

                if ($(this).hasClass('outofnetwork')) {
                    $('#btnToClick').val(btnID);
                    $("#divWarning").dialog("open");
                }
                else {
                    $(btnID).click();
                }

                return false;
            });

            $('#btnProceed').on('click', function () {
                console.log($('#btnToClick').val());
                $('#ctl00_ContentPlaceHolder1_btnProceedClicked').val("true");
                $($('#btnToClick').val()).click();
            });
        })

在GridView_RowCommand事件处理程序中添加的代码,用于确定是否应该重定向。

                //Grabs the index from the end of the CommandArgument and uses to get the selected row in the GridView.
                int selectedIndex = Convert.ToInt32(e.CommandArgument.ToString().Split('|')[3].ToString());
                GridViewRow selectedRow = gridResults.Rows[selectedIndex];

                //Redirects if an "IN NETWORK" row was selected or and "OUT OF NETWORK" row was selected and the user clicked on Proceed to Next Step button in the popup dialog.
                if (selectedRow.Cells[11].Text != "OUT OF NETWORK" || (selectedRow.Cells[11].Text == "OUT OF NETWORK" && btnProceedClicked.Value == "true"))
                {
                    Redir(oEventFlow.SiteSelectionRedirect);
                }
另一答案

所以我认为你的问题是事件默认是点击asp按钮的回发。这就是为什么模态在打开时会闪烁,因为它打开然后页面重新加载。你可以解决这个问题

  • 1)在openDialog()函数结束时或使用e.preventDefault返回false。这涉及以下堆栈溢出question
  • 2)使用UpdatePanel
  • 3)使用常规html按钮(需要指定type="button"

preventDefault

所以你在aspx的服务器上有对话运行

<div style="display:none" id="dialog_warning" title="Out of Network" runat="server">
        <p class="CallOut">
            <asp:Label ID="lblOutOfNetworkMsg" HtmlEncode="false" runat="server"></asp:Label>
        </p>
        <p style="text-align:center;">
        <asp:Button style="width:200px; background-color:#E9967A; font-family:Arial; font-size:11px; font-weight:bold; border:2px solid black;" ID="btnBack" runat="server" OnClientClick="return true;" CausesValidation="false" /><br /><br />
        <asp:Button style="width:200px; background-color:#E9967A; font-family:Arial; font-size:11px; font-weight:bold; border:2px solid black;" ID="btnProceed" runat="server" OnClientClick="return false;" CausesValidation="false" />
        </p>
</div>

<script>
    // your js code
    // etc

    function OpenDialog(e) {
        $("#dialog-warning").dialog("open");

        // prevent default
        e.preventDefault();
    }
</script>

然后在GridView_RowDataBound

btn.Click += new EventHandler(delegate (object _s, EventArgs _e){

   // your code
   // etc

   // add client click           
   dialog_warning.OnClientClick = "OpenDialog()";

});

UpdatePanel

你只需将按钮包装在更新面板中以及任何可由代码隐藏更新的按钮,但由于除了模态弹出窗口之外什么都没有改变,你可以将按钮保留在面板中

<asp:UpdatePanel id="UpdatePanel1" runat="server" updatemode="Conditional">
    <ContentTemplate>
       <asp:Button id="button1" runat="server" Text="Click me!" />
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger controlid="button1" eventname="Click" />
    </Triggers>
</asp:UpdatePanel>

html button

动态添加一个html按钮,在你的rowdatabound中设置onclick

e.Row.Cells[i].Controls.Add(
    new LiteralControl("<button type='button' onclick='OpenDialog()'></button>")
);

以上是关于jQuery对话框在屏幕上闪烁,而不是留在用户输入的主要内容,如果未能解决你的问题,请参考以下文章

避免在调整大小时移动其控件的对话框上闪烁

如何解决由于加载图像而导致屏幕闪烁的问题?

选择日期后,jQuery UI datepicker会使屏幕滚动到顶部

DIV 内容显示在页面而不是 JQuery 对话框上

在 iOS 8 中获取用户位置警报不会留在屏幕上

模式对话框与非模式对话框 消息处理顺序