ICallbackEventHandler使用
生活随笔
收集整理的這篇文章主要介紹了
ICallbackEventHandler使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
后端:頁面需繼承ICallbackEventHandler
protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){string strRefrence = string.Empty;strRefrence = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveDataFromServer", "context"); // 這里ReceiveDataFromServer為客戶端接收回調結果的JS方法,含一個傳入參數string strCallBack = string.Empty;strCallBack = "function CallBackToTheServer(arg, context) {" + strRefrence + "};";Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallBackToTheServer", strCallBack, true); // CallBackToTheServer,JS方法,發出回調請求 }}#region ICallbackEventHandler Membersprivate string strTimeFormat;public string GetCallbackResult(){if (strTimeFormat != "" && strTimeFormat == "12"){return DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss(12小時制)");}else{return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss(24小時制)");}}public void RaiseCallbackEvent(string eventArgument){strTimeFormat = eventArgument;}#endregion前端:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script>function ReceiveDataFromServer(valueReturnFromServer) {document.getElementById("ServerTime").innerHTML = valueReturnFromServer;}function GetServerTime(format) {CallBackToTheServer(format, "");}</script><title></title> </head> <body><form id="form1" runat="server"><div><asp:Button ID="btnShow12" runat="server" Text="獲取服務器時間(12小時制)" OnClientClick="javascript:GetServerTime(12);return false;" /><br /><asp:Button ID="btnShow24" runat="server" Text="獲取服務器時間(24小時制)" OnClientClick="javascript:GetServerTime(24);return false;" /><br /><br /><span id="ServerTime"><%= DateTime.Now.ToString("yyyy-MM-dd HHHH:mm:ss") %></span> </div></form> </body> </html>?
轉載于:https://www.cnblogs.com/shikyoh/p/3479900.html
總結
以上是生活随笔為你收集整理的ICallbackEventHandler使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jQuery实战读书笔记(第五章)
- 下一篇: Mac安装与配置idea及tomcat