How can I confirm and then disable the button in asp.net/javascript

I have a standard ASP.NET 2.0 webpage with a Delete button on it. What I need, and I can’t understand how to do it, when the user clicks the delete button, a confirmation dialog box appears asking the user: "Are you sure?". If the user says yes, I want to disable the Delete button and perform a postback that will run the server-side code deleteButton_Click.

Here is the tag:

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

Here is javascript (in jquery) to handle client-side click:

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" ), "" );
    }
} );

, , . postback , deleteButton_Click. javascript __doPostBack.

UseSubmitBehavior = "false" deleteButton, .

, , ASP.NET . , ?

,

Craig

+3
10

, .

javascript :

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

:

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

, "name" , doPostBack.

+1

StackOverflow. :

... ...

:)

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());
}

:)

+1
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;}");
+1

, postback javascript, OnClientClick .

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

, javascript, , - :

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

, return, , , ASP , "return false".

+1

Javascript:

deleteButton.disabled = true;

#:

deleteButton.Enabled = false;
0

, deleteButton_Click ( , ).

<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 ...
}
0

, click . , , , , . Page_Load, / ( ), . __doPostBack().

0

, . , , .

, <% @Page EnableEventValidation = "false" % > ASPX , ( ​​ true). , , , . . , XSS, JavaScript . .

0
$(":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;
    }
});
0

JS, .

Here's an example that works, I would move JavaScript to another location, such as an existing .js file or main tag.

<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)" />
0
source

Source: https://habr.com/ru/post/1711346/


All Articles