简体   繁体   English

C#asp.net确认在Ajax调用中不起作用

[英]C# asp.net Confirm Not working in Ajax Call

Apologies beforehand, this is my first time coding using javascript. 抱歉,这是我第一次使用javascript编码。 All of this I have written based off of other questions I found on here. 我写的所有这些都是基于我在这里找到的其他问题。

I have a call to a webmethod that checks whether a page is dirty and returns a boolean: 我有一个调用web方法的电话,该方法检查页面是否脏了并返回布尔值:

<script type="text/javascript">
    function OnConfirm() {
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            url: "UserManagement.aspx/IsDirty",
            dataType: "json",
            data: '{}',
            success: function OnDirtySuccess(result) {
                if (result.d.toString() == "true") {
                    return confirm("You have unsaved changes, select OK to discard these changes without saving.");
                }
                else {
                    return false;
                }

                return true;
            },
            failure: function (response) {
                return true;
            }

        });
    }
</script>

I have a button that calls the script: 我有一个调用脚本的按钮:

<asp:Button ID="btnAddNewUser" CausesValidation="false" runat="server" Text="Add New User" OnClick="btnAddNewUser_Click" OnClientClick="return OnConfirm();" Width="140px" />

The method call is working correctly, I only see the confirm dialog when the current user has been modified. 该方法调用正常运行,仅当修改了当前用户后,我才看到确认对话框。 My issue is when I click 'cancel' the event for btnAddNewUser_Click is still firing. 我的问题是,当我单击“取消”时,btnAddNewUser_Click的事件仍在触发。

A different approach based on another question posted here, that did not work for me: 基于此处发布的另一个问题的另一种方法对我不起作用:

<script type="text/javascript">
    function OnConfirm() {
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            url: "UserManagement.aspx/IsDirty",
            dataType: "json",
            data: '{}',
            success: function OnDirtySuccess(result) {
                if (result.d.toString() == "true") {
                    if (!confirm("You have unsaved changes, select OK to discard these changes without saving."))
                        return false;
                    //return confirm("You have unsaved changes, select OK to discard these changes without saving.");
                }
                else {
                    return false;
                }

                return true;
            },
            failure: function (response) {
                return true;
            }

        });
    }
</script>  
<asp:Button ID="btnAddNewUser" CausesValidation="false" runat="server" Text="Add New User" OnClick="btnAddNewUser_Click" OnClientClick="if (! OnConfirm()) return false;" Width="140px" />

Here, I modified the OnClientClick to if (! OnConfirm()) return false; 在这里,我将OnClientClick修改为if(!OnConfirm())返回false; and I tried using both versions of the confirm code inside OnConfirm(). 并且我尝试在OnConfirm()中使用两种版本的确认代码。 However, when the confirm dialog pops up and I hit cancel, the OnClick event still fires. 但是,当弹出确认对话框并且我单击“取消”时,仍会触发OnClick事件。

I've tried changing the OnClientClick function to (just to simplify the code and test): 我尝试将OnClientClick函数更改为(只是为了简化代码和测试):

<script type="text/javascript">
    function OnConfirm() {
        return confirm('Test ok/cancel?');
    }
</script>

and the Ok/Cancel of the confirmation dialog is working properly. 并且确认对话框的“确定/取消”工作正常。

What am I missing with my OnConfirm function? 我的OnConfirm函数缺少什么?

Check this link .It has exactly the same answer of your question 检查此链接。它与您的问题的答案完全相同

click me 点击我

Update for you. 为您更新。

Use POST to pass the data 使用POST传递数据

 <asp:Button ID="btnAddNewUser" ClientIDMode="Static" CausesValidation="false" runat="server" Text="Add New User"  Width="140px" />

and

<script type="text/javascript">
    $(function() {

        $('#btnAddNewUser').click(function(e) {
            e.preventDefault();
            //Make your ajax call
            var params = {};
            params.parameter = "passing string data";
            $.ajax({
                type: "POST",
                url: "YourPageName.aspx/MyFunction1",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(params),
                success: function(res) {
                    alert(res.d);
                },
                error: function(res) {
                }
            });
        });
    });
</script>

and YourPageName.aspx.cs file 和YourPageName.aspx.cs文件

        [WebMethod]
        public static string MyFunction1(string parameter)
        {

            return parameter;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM