2009-11-19 37 views
0

當談到AJAX和一般的JavaScript時,我很垃圾。在System.web.services.WebMethod中傳遞變量的C#

我有一個WebMethod:

[System.Web.Services.WebMethod] 公共靜態字符串DumpClients() {}

我有一個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); 

這工作得很好,(我承認我沒有完全理解這一點),直到我需要從頁面訪問下拉列表。由於這是一種靜態方法,我無法直接獲取它,所以我想我可以通過web方法將值傳回。

小問題是我不知道如何。我一直在谷歌搜索,但沒有得到任何快速。我正在通過一本JQuery書籍瞭解更多的基礎知識,但在這一刻,這超出了我的想法。

我很感激任何和所有的幫助和建議,抱歉,我可能會問一些愚蠢的問題。

回答

1

所以我決定我會完全錯誤的方式,尋找更好的方式來調用該方法,並發現此解決方案:

$("#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) 
{ 
} 

希望這可以幫助其他人。