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

C在system.web.services.webmethod中传递变量

  •  0
  • Jon  · 技术社区  · 16 年前

    一般来说,当涉及到Ajax和JavaScript时,我都是垃圾。

    我有一个WebMethod:

    [系统.web.services.webmethod] 公共静态字符串转储客户端() _

    我在JS文件中有此代码:

    mainScreen.DumpClients = function() {
        $('#runclientdumpbtn').attr("disabled", "true");
        mainScreen.clientDiv.innerHTML = "";
        $("#loadingimageclientdump").show();
        PageMethods.DumpClients(mainScreen.DumpClientsCallback, mainScreen.DumpClientsFailed);
    }
    mainScreen.DumpClientsCallback = function(result) {
        if (result) {
            $("#loadingimageclientdump").hide();
            mainScreen.clientDiv.innerHTML = result;
            $('#runclientdumpbtn').removeAttr("disabled");
        }
    };
    mainScreen.DumpClientsFailed = function(error, userContext, methodName) {
        if (error) {
            // TODO: add your error handling
            $("#loadingimageclientdump").hide();
            mainScreen.clientDiv.innerHTML = error.get_message();
            $('#runclientdumpbtn').removeAttr("disabled");
        }
    };
    
    Sys.Application.add_load(applicationLoadHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequestHandler);
    

    这很好,(我承认我完全不理解这一点),直到我需要从页面访问DropDownlist。因为它是一个静态方法,所以我不能直接得到它,所以我想我可以通过webmethod返回值。

    小问题是我不知道怎么做。我一直在谷歌搜索它,但没有得到任何快速。我正在通过一本jquery书来了解更多的基础知识,但这在此刻已经超出了我的能力范围。

    我很感激所有的帮助和建议,很抱歉我可能问了一个愚蠢的问题。

    1 回复  |  直到 10 年前
        1
  •  1
  •   Jon    16 年前

    所以我决定我走错了方向,寻找一种更好的方法来调用这个方法,并找到了这个解决方案:

      $("#runclientdumpbtn").click(function() {
            var selectedreporttype = $("#<%= dropdownpageID %>").val();
    
            $.ajax({
                type: "POST",
                url: "default.aspx/ExtractContacts",
                data: "{outputtype:'" + selectedreporttype + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnContactSuccess,
                failure: OnContactFailure,
                error: OnContactFailure
            });
    
            startContact();
        });
    
    
    
    
    [WebMethod()]
    public static string ExtractContacts(string outputtype)
    {
    }
    

    希望这能帮助别人。