我想CsharpFunction从JavaScript 调用,这是一个C#函数。我尝试了下面的代码,但是无论JavaScript条件是True还是False,都CsharpFunction被调用了!
CsharpFunction
True
False
JavaScript代码:
if (Javascriptcondition > 0) { <%CsharpFunction();%> }
后面的C#代码:
protected void CsharpFunction() { // Notification.show(); }
如何从JavaScript调用C#函数?
您可以使用Web方法和Ajax:
<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 }