后台代码:

 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" />

 

相关文章:

  • 2022-12-23
  • 2021-09-05
  • 2022-01-04
  • 2021-07-26
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-11
  • 2022-12-23
  • 2021-12-23
  • 2021-09-04
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案