我相信你必須爲此使用AJAX。看看這裏:https://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx
ReloadData會在你的代碼一個WebMethod後面,將是這個樣子:
[System.Web.Services.WebMethod]
public void ReloadData()
{
//here is the code
}
然後從客戶端,你會做這樣的事情:
function GetData() {
$.ajax({
type: "POST",
url: "CS.aspx/ReloadData",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function(response) {
alert(response.d);
}
});
}
CS.aspx
是您的網頁的名稱。
繼續從您的評論下面;如果你不想使用jQuery那麼你的Javascript代碼會是這個樣子:
var xhr = new XMLHttpRequest();
xhr.open('GET', 'CS.aspx/ReloadData');
xhr.onload = function() {
if (xhr.status === 200) {
alert('Successful');
}
else {
alert('Request failed. Returned status of ' + xhr.status);
}
};
xhr.send();
[Pagemethods在asp.net]的可能的複製(https://stackoverflow.com/questions/4313532/pagemethods-in -asp-net) – EdSF
nope這不是重複的,因爲這裏調用了javascript,但不會在服務器端方法 –
再次閱讀。當天結束時,您正在使用客戶端腳本向某個服務器資源發出請求。「Web Forms」具有'PageMethods'作爲您可以使用的_option_,並且需要一些設置。 – EdSF