代码之家  ›  专栏  ›  技术社区  ›  Craig

如何确认并禁用ASP.NET/javascript中的按钮

  •  3
  • Craig  · 技术社区  · 16 年前

    我有一个标准的ASP.NET 2.0网页,上面有一个“删除”按钮。我需要的是当用户按下“删除”按钮时,会弹出一个确认对话框,询问用户“确定吗?”。如果用户选择“是”,则我希望禁用“删除”按钮并执行回发,以运行服务器端代码“删除”按钮。

    这是标签:

    <asp:Button ID="deleteButton" Text="Delete" OnClick="deleteButton_Click" runat="server" />
    

    下面是(jquery中)处理客户端单击的javascript:

    var deleteButton = $(input.eq(0));
    deleteButton.click( function()
    {
        var a = confirm( "Are you sure you want to delete this item?" );
        if ( a == false )
        {
            return false;
        }
        else
        {
            deleteButton.attr( "disabled", "disabled" );
            __doPostBack( deleteButton.attr( "id" ), "" );
        }
    } );
    

    确认对话框按预期工作,禁用也工作正常。表单没有回发,但它没有运行DeleteButton_Click事件处理程序。页面上确实存在\doPostback javascript代码。

    我可以将useSubmitBehavior=“false”添加到DeleteButton标记中,但它将忽略确认对话框的答案。

    所以也许我对ASP.NET的要求太高了。有什么办法可以让这个工作吗?

    谢谢,

    克雷格

    10 回复  |  直到 16 年前
        1
  •  1
  •   George    16 年前
    btnSubmit.Attributes.Add("onclick", "if(confirm(\"Are you sure you want to delete this item?\" ){this.disabled=true;" + ClientScript.GetPostBackEventReference(btnSubmit, "").ToString() + "}else{return false;}");
    
        2
  •  1
  •   Community Mohan Dere    8 年前

    看看这个stackoverflow问题,我发现它非常有用:

    Disable button on form submission

    添加确认逻辑…你应该被安排…

    我去了一个头,为你和我写了一个扩展方法:)

    public static void ConfirmThenDisableButtonOnClick(this Button buttonControl, string confirmMessage, string clientFunction)
    {
        StringBuilder sb = new StringBuilder();
    
        // If the page has ASP.NET validators on it, this code ensures the
        // page validates before continuing.
        if (buttonControl.CausesValidation && !String.IsNullOrEmpty(buttonControl.ValidationGroup))
        {
            sb.Append("if ( typeof( Page_ClientValidate ) == 'function' ) { ");
            sb.AppendFormat(@"if ( ! Page_ClientValidate('{0}') ) {{ return false; }} }} ", buttonControl.ValidationGroup);
        }
    
        //pop confirm, if confirm==true, disable button
        sb.AppendFormat("if(confirm('{0}\')){{this.disabled=true;", confirmMessage.Replace("\"","").Replace("'",""));
    
        // If a secondary JavaScript function has been provided, and if it can be found,
        // call it. Note the name of the JavaScript function to call should be passed without
        // parens.
        if (!String.IsNullOrEmpty(clientFunction))
        {
            sb.AppendFormat("if ( typeof( {0} ) == 'function' ) {{ {0}() }};", clientFunction);
        }
    
        // GetPostBackEventReference() obtains a reference to a client-side script function 
        // that causes the server to post back to the page (ie this causes the server-side part 
        // of the "click" to be performed).
        sb.Append(buttonControl.Page.ClientScript.GetPostBackEventReference(buttonControl, ""));
    
        // if confirm==false do nothing
        sb.Append(";}else{return false;}");
    
        // Add the JavaScript created a code to be executed when the button is clicked.
        buttonControl.Attributes.Add("onclick", sb.ToString());
    }
    

    还添加了验证检查:)

        3
  •  1
  •   Craig    16 年前

    感谢您的反馈,但这是一个相当简单的解决方案。

    javascript行应该是:

    __doPostBack( deleteButton.attr( "name" ), "" );
    

    而不是:

    __doPostBack( deleteButton.attr( "id" ), "" );
    

    我没有意识到“name”属性是doPostback方法正在寻找的属性。

        4
  •  1
  •   myQwil    12 年前

    您可以使用onclientclick来完成相同的事情,而不是通过javascript进行回发。

    <asp:Button OnClientClick="if (confirm('Are you sure?')) {this.disabled = true;} else {return false;}" />
    

    或者,如果您想通过javascript执行更多的操作,而不是简单地禁用按钮,您可以这样说:

    <asp:Button OnClientClick="return DisableOnConfirm();" />
    

    您只需要确保用RETURN语句的形式对其进行了措辞,这样当您不希望ASP函数运行时,该语句最终会说“RETURN FALSE;”。

        5
  •  0
  •   Theofanis Pantelides    16 年前

    Javascript:

    deleteButton.disabled = true;
    

    C:

    deleteButton.Enabled = false;
    
        6
  •  0
  •   ppiotrowicz    16 年前

    您可能应该在DeleteButton_ck事件中禁用服务器端的按钮(因为您将进行回发,所以您需要重新创建页面)。

    <asp:Button ID="deleteButton" Text="Delete" OnClick="deleteButton_Click" runat="server" OnClientClick="javascript:return confirm('are you sure blah blah blah ...')" />
    

    在后面的代码中:

    private void deleteButton_Click(object sender, EventArgs e) {
        deleteButton.Enabled = false;
        // and the rest of event handling ...
    }
    
        7
  •  0
  •   Knickerless-Noggins    16 年前

    点击事件没有触发有点奇怪。我想这是因为按钮被禁用了,但通常只有当按钮在服务器上被禁用时才会发生,而不仅仅是在客户机上。虽然在页面加载函数中不是理想的解决方案,但是您可以检查回发和/或异步回发,并确定回发的目标(在本例中是您的按钮),然后从那里调用一些方法。您还可以使用uuDoPostback()函数传入参数。

        8
  •  0
  •   SolutionYogi Eric Lippert    16 年前

    最初,我不确定您是在谈论客户端单击事件还是服务器端单击事件。我认为您应该明确声明服务器端单击事件没有触发。

    无论如何,请在您的ASPX页上尝试<%@page enableEventValidation=“false”%>,查看它是否工作(默认情况下设置为true)。我不记得这个属性的本质是有效的,但是这个特性确保服务器端事件只在合法的情况下被触发。例如,假设您的页面存在XSS漏洞,黑客注入了javascript来调用删除按钮。此属性有助于防止这些攻击。

        9
  •  0
  •   cllpse    16 年前
    $(":submit").click(function ()
    {
        $(this).attr("disabled", true); // disable input
    
        if (confirm("Press OK to submit"))
        {
            __doPostBack(this.id, ""); // if user presses OK; submit
        }
        else
        {
            // If user presses cancel; enable input and stop submit
            $(this).attr("disabled", false);
    
            return false;
        }
    });
    
        10
  •  0
  •   Tony L. ccalboni    10 年前

    我决定让它成为一个可以从任何按钮调用的JS函数。

    这是一个有效的示例,不过,我会将javascript移动到其他位置,例如现有的.js文件或head标记。

    <script>
        function confirmPostback(button) {
            if (confirm('Are you sure?')) {
                button.disabled = true;
                __doPostBack(button.name, '')
                return true;
            } else {
                return false;
            }
        }
    </script>
    
    <asp:Button ID="TestPostback" runat="server" Text="Test Postback"
            UseSubmitBehavior="false" OnClientClick="return confirmPostback(this)" />
    
    推荐文章