Asp.net2.0 's client callback is a very exciting way to let us control what data is submitted to the server without submitting the entire page, and the server simply returns the data you need instead of sending it back to the entire page.
First of all, we have to say a very important method: Getcallbackeventrefernce. I write my understanding out, may be wrong, please point out, thank you very much!
GetCallbackEventReference first realizes that the client script has the ability to pass parameters to the server-side RaiseCallbackEvent method, and then returns the value of the RaiseCallbackEvent method to you in A parameter that is registered in the Getcallbackeventrefernce method (it is also a script that you want to write on the client). Call getcallbackeventrefernce You must pass the client script to him two parameters, one is to pass to the RaiseCallbackEvent event value, one is the context.
The meaning of his argument is as follows:
First: a page or server control that implements the ICallbackEventHandler excuse, and writes this to the front page.
Second: Represents the value that you want to pass from the client to the server RaiseCallbackEvent method
Third: You want to write a JS function on the client, at the same time, the server will also pass the computed data to the function as the parameter of this function.
Fourth: Context specific what meaning I am not very clear getcallbackeventrefernce sent to the customer, the end of the code is like this:
WebForm_DoCallback ('__Page',arg,ReceiveServerData,context,null,false)
So what can we do to call him from the client? See the three-way method:
The first: Write a public string in the background and assign the value to him in the Page_Load: =page.clientscript.getcallbackeventreference (this, "message", " Showservertime "," context ") Note that this is page.clientscrip because he will return a Clientscriptmanager,clientscriptmanager client script. Then <%= the public background string in the OnClick event of a button in the foreground%> do a little experiment code as follows:
Front desk servertime.aspx: In order to easily remove a lot of useless html
<%@ page language="C#" CodeFile="ServerTime.aspx.cs" Inherits="ServerTime_aspx" %>
<title>Server Time</title>
<script language="javascript">
function GetServerTime()
{
var message = '';
var context = '';
<%=sCallBackFunctionInvocation%>
}
function ShowServerTime(timeMessage, context) {
alert('现在服务器上的时间是:\n' + timeMessage);
}
</script>
<body>
<form id="MainForm" runat="server">
<input type="button" value="得到服务器端时间" onclick="GetServerTime();" />
</form>
</body>