2013-07-17 73 views
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); 
     } 

我在代碼behind.I函數想調用C#編寫的代碼behind.Please功能發送的代碼使用jQuery來訪問它...接入功能從aspx頁面

回答

2

你不能打電話任何函數寫在你的代碼後面的JavaScript。

您只能調用靜態webmethods。

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
<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 
}