从JavaScript触发对c# asp.net代码的回调
本文关键字:net 代码 回调 asp JavaScript | 更新日期: 2023-09-27 18:12:27
据我所知,对后端代码的CallBack不会导致页面重新加载(就像PostBack一样)。然而,我不知道如何从我的客户端JavaScript完成回调。
或者是否有其他方法可以只运行一次backen函数?
http://api.jquery.com/jQuery.ajax/
看看Dave Ward写的这篇文章-
http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/它将带您逐步完成从jQuery到c#的调用设置过程
当你创建一个HTML对象,如HTML Button add runat="server"作为属性时,asp.net将创建一个javascript函数__doPostBack。你可以使用这个函数来调用服务器对象。
<html>
<body>
<form id="form1" runat="server">
<asp:Label ID="lblCounter" runat="server" Text="Counter:"></asp:Label>
<br />
<br />
<input id="btnAddCounter" type="button" value="Add Counter" runat="server" />
<br />
<br />
<a onclick="__doPostBack('btnAddCounter','')" style="cursor: pointer;">Invoke Postback (it's just like push the Button)</a>
</form>
</body>
</html>
后面的代码:
Partial Class test_testpostback
Inherits System.Web.UI.Page
Shared counter As Integer
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
lblCounter.Text = "[This will display counter]"
End Sub
Protected Sub btnAddCounter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddCounter.ServerClick
counter = counter + 1
lblCounter.Text = "Counter: " & counter
End Sub
End Class