頁面代碼:
<script type="text/javascript">
function ReceiveServerData(rValue)
{
alert(rValue);
}
</script>
<input id="Button1" type="button" value="button" onclick ="CallServer('Client')" />
服務端代碼:public partial class back2 : System.Web.UI.Page,System .Web .UI .ICallbackEventHandler
{
public string CallBackValue = null;
protected void Page_Load(object sender, EventArgs e)
{
}
// 註冊指令碼到前台頁面
protected void Page_PreRender(object sender, EventArgs e)
{
RegClientScript();
}
// javascript函數(伺服器端事件的用戶端回調)
protected void RegClientScript()
{
ClientScriptManager cs = Page.ClientScript;
string jstxt=@"
function CallServer(msgid)
{
" + cs.GetCallbackEventReference(this, "msgid", "ReceiveServerData", null) + @";
}";
cs.RegisterStartupScript(this.GetType(), "callserver", jstxt, true);
}
//ICallbackEventHandler介面
//把值傳到前台
string ICallbackEventHandler.GetCallbackResult()
{
return CallBackValue + ",Server";
}
//按受前台的參數
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
this.CallBackValue = eventArgument;
}
}
運行結果: (實現非同步互動,頁面不重新整理)