0
public void storescore()
        {
            ScoreBAL bal = new ScoreBAL();
            ScoreBOL bol = new ScoreBOL();
            bol.userid = uid;
            bol.time =lbltime.Text;
            bol.scores = lblmark.Text;
            bol.dates = DateTime.Now;
            bal.insertscore(bol);
        }

我在后面的代码中有一个函数。我想调用后面用 c# 代码编写的函数。请发送代码以使用 Jquery 访问它...

4

2 回答 2

2

您不能在 javascript 中调用在您的代码中编写的任何函数。

您只能调用静态网络方法。

aspx:

<asp:ScriptManager ID="smMain" runat="server" EnablePageMethods="true" />
<script type="text/javascript">
    function StoreScore() {
        PageMethods.storescore();
    }
</script>

CS:

[System.Web.Services.WebMethod]
public static void storescore()
{
   ScoreBAL bal = new ScoreBAL();
   ScoreBOL bol = new ScoreBOL();
   bol.userid = uid;
   bol.time =lbltime.Text;
   bol.scores = lblmark.Text;
   bol.dates = DateTime.Now;
   bal.insertscore(bol);
}
于 2013-07-17T08:50:35.987 回答
1
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
<script type="text/javascript">
    $.ajax({
                type: "POST",
                url: "/PageName/storescore",
                 contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: false,
                success: function (data) {                
                },
                error: function (msg) {
                    alert(msg);
                }
            });
</script>

后面的代码:

using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
[WebMethod]
public static void StoreScore()
{
//do something
}
于 2013-07-17T09:00:13.527 回答