[英]C# function called in javascript and it's execute before the page load. why?
我試圖在 javascript 中調用 c# 函數,所以我使用了這個: var a = <%=MYC#FUNCTION()%>
,但奇怪括號中的函數甚至在我的頁面加載之前執行。 就像執行函數是我代碼的重中之重。 我希望該函數在我的 javascript 代碼中調用它時執行。 請幫助我,我在學校的項目需要這個。 我嘗試使用它,但我並沒有真正理解這一點->
<script type="text/javascript"> //Default.aspx
function DeleteKartItems() {
$.ajax({
type: "POST",
url: 'Default.aspx/DeleteItem',
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#divResult").html("success");
},
error: function (e) {
$("#divResult").html("Something Wrong.");
}
});
}
</script>
[WebMethod] //Default.aspx.cs
public static void DeleteItem()
{
//Your Logic
}
您誤解了請求/響應的生命周期。 在您的代碼中,執行順序將是
如果您想在頁面加載后執行“MYC#FUNCTION()”。 然后你需要看看AJAX。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.