访问函数来自aspx页面

时间:2013-07-17 08:46:14

标签: asp.net

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访问它...

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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
}