頁面代碼:
<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;
}
}