【问题标题】:Execute javascript/jquery code OnClick and conditional post back to ASP function执行 javascript/jquery 代码 OnClick 并有条件地回传到 ASP 函数
【发布时间】:2012-02-28 03:19:18
【问题描述】:

jQuery 新手问题在这里...我有一个 jQuery 对话框,显示带有“确定”或“取消”的警告,并根据他们单击的结果需要执行服务器端 ASP onClick 事件。

我尝试按照以下方式编写它:

            "OK": function () {
                $(this).dialog("close");
                return true;
            },
            Cancel: function () {
                $(this).dialog("close");
                return false;

但它从不回传到 asp 服务器端方法。

我在这里尝试完成的工作是否偏离了基础,是否有标准的“最佳实践”类型的方式来实现此类功能?

【问题讨论】:

    标签: javascript jquery asp.net


    【解决方案1】:

    您必须使用适当的eventTargeteventArgument 调用__doPostBack() 方法来调用其服务器点击处理程序。

      "OK": function () {
           $(this).dialog("close");
           __doPostBack('serverElementId', 'arguments');
       }
    

    您可以查看以下链接以了解 ASP.Net 回发机制的工作原理。

    http://wiki.asp.net/page.aspx/1082/dopostback-function/

    【讨论】:

      【解决方案2】:

      假设您想在“OK”时执行 POST,您需要添加如下内容:

      "OK": function () {
        $.post('someurl.asp', {data}, function() {
          // what to do after the post
        }
        $(this).dialog("close");
        return true;
      }
      

      就目前而言,您的代码只是关闭对话框。

      【讨论】:

        【解决方案3】:

        我没有使用 asp.net 的经验,但您可以使用 AJAX 请求将数据发布到您的服务器端脚本:

                "OK": function () {
                    $(this).dialog("close");
                    $.ajax({
                        url      : 'myscript.asp',
                        type     : 'get',
                        dataType : 'json',
                        data     : { state : 'OK' },
                        success  : function (serverResponse) {
                            //you can now access the serverResponse
                        },
                        error    : (jqXHR, errorText, errorThrown) {
                            //always make sure to handle your errors
                        }
                    });
                    return true;
                },
                Cancel: function () {
                    $(this).dialog("close");
                    $.ajax({
                        url      : 'myscript.asp',
                        type     : 'get',
                        dataType : 'json',
                        data     : { state : 'Cancel' },
                        success  : function (serverResponse) {
                            //you can now access the serverResponse
                        },
                        error    : (jqXHR, errorText, errorThrown) {
                            //always make sure to handle your errors
                        }
                    });
                    return false;
                }
        

        $.ajax() 的文档:http://api.jquery.com/jquery.ajax

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-08-25
          • 1970-01-01
          • 2013-02-03
          • 2021-06-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多