Confirm postback OnClientClick button ASP.NET

后端 未结 10 1265
梦毁少年i
梦毁少年i 2020-12-01 15:47


        
相关标签:
10条回答
  • 2020-12-01 16:12

    Try this:

    <asp:Button runat="server" ID="btnDelete" Text="Delete"
       onClientClick="javascript:return confirm('Are you sure you want to delete this user?');" OnClick="BtnDelete_Click" />
    
    0 讨论(0)
  • 2020-12-01 16:14

    The code is like this:

    In Aspx:

    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" CausesValidation=true />
    

    in Cs:

    protected void Page_Load(object sender, System.EventArgs e)
    {
         if (!IsPostBack)
         {
             btnSave.Attributes["Onclick"] = "return confirm('Do you really want to save?')";          
         }
    }
    
    protected void btnSave_Click(object sender, EventArgs e){
        Page.Validate();
        if (Page.IsValid)
        {
           //Update the database
             lblMessage.Text = "Saved Successfully";
        }
    }
    
    0 讨论(0)
  • 2020-12-01 16:23

    This is a simple way to do client-side validation BEFORE the confirmation. It makes use of the built in ASP.NET validation javascript.

    <script type="text/javascript">
        function validateAndConfirm() {
            Page_ClientValidate("GroupName");  //'GroupName' is the ValidationGroup
            if (Page_IsValid) {
                return confirm("Are you sure?");
            }
            return false;
        }
    </script>
    
    <asp:TextBox ID="IntegerTextBox" runat="server" Width="100px" MaxLength="6" />
    <asp:RequiredFieldValidator ID="reqIntegerTextBox" runat="server" ErrorMessage="Required"
        ValidationGroup="GroupName"  ControlToValidate="IntegerTextBox" />
    <asp:RangeValidator ID="rangeTextBox" runat="server" ErrorMessage="Invalid"
        ValidationGroup="GroupName" Type="Integer" ControlToValidate="IntegerTextBox" />
    <asp:Button ID="SubmitButton" runat="server" Text="Submit"  ValidationGroup="GroupName"
        OnClick="SubmitButton_OnClick" OnClientClick="return validateAndConfirm();" />
    

    Source: Client Side Validation using ASP.Net Validator Controls from Javascript

    0 讨论(0)
  • 2020-12-01 16:24

    Using jQuery UI dialog:

    SCRIPT:

    <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
    <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
    <script>
     $(function () {
    
                $("#<%=btnUserDelete.ClientID%>").on("click", function (event) {
                    event.preventDefault();
                    $("#dialog-confirm").dialog({
                        resizable: false,
                        height: 140,
                        modal: true,
                        buttons: {
                            Ok: function () {
                                $(this).dialog("close");
                                __doPostBack($('#<%= btnUserDelete.ClientID %>').attr('name'), '');
                            },
                            Cancel: function () {
                                $(this).dialog("close");
                            }
                        }
                    });
                });
     });
    </script>
    

    HTML:

    <div id="dialog-confirm" style="display: none;" title="Confirm Delete">
        <p><span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>Are you sure you want to delete this user?</p>
    </div>
    
    0 讨论(0)
提交回复
热议问题