js回掉页面后台代码-简单demo
后台代码:
public partial class WebForm1 : System.Web.UI.Page, ICallbackEventHandler { protected void Page_Load(object sender, EventArgs e) { RenderCallbackEvent(); } private void RenderCallbackEvent() { string js = "function CallServer(){ "; string refJs = this.ClientScript.GetCallbackEventReference(this, "arg", "receiveData", null, true);//第二个参数必须与前台定义的一致 js += refJs + ";}"; this.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString("N"), js, true); } int tmp = 0; public void RaiseCallbackEvent(string requestArg) { tmp = Convert.ToInt32( requestArg) + 2; } public string GetCallbackResult() { return tmp.ToString(); } }
前台代码:
<script> var arg = 3;//必须与后台注册的参数arg名称相同 function receiveData(data) { if (console) { console.log(data); } } </script> <input onClick="CallServer();" type="button" value="callback" />