"No Refresh page" is just an inexact description of the effect (there are other ways to do this), more specifically, to use JavaScript on the page to invoke a server-side method and then process the returned data. The most standard way to achieve it is, of course, XMLHTTP. But programmers are lazy guys, and everyone wants a more convenient way, or better packaging. For example, Lostinet's Rane is a good package for XMLHTTP.
Finally, in ASP.net 2.0, we can easily do this. Any control that implements the System.Web.UI.ICallbackEventHandler interface on the server side can handle the requests and data passed from the JS script on the page through the RaiseCallbackEvent () method, and after processing, The results are then passed back to the page. The bottom of this ability is still xmlhttp.
Here's a simple demo:
On the page, we put two text boxes and a button:
<INPUT id="txtMessage">
<INPUT onclick="callToServer();" type="button" value="Call to Server">
Result : <INPUT id="txtResult" >
When the button is clicked, the JS script method Calltoserver () is called, and the JS script is as follows:
function callToServer()
{
var param = document.getElementById("txtUsername").value;
var context = "";
<% = ClientScript %>
}
function handleResultFromServer(result, context)
{
document.getElementById("txtResult").value = result;
}
The Handleresultfromserver () method is responsible for writing the data returned from the server to the Txtresult text box.
Then look at the server-side code:
public partial class Default_aspx : System.Web.UI.ICallbackEventHandler
{
private String ClientScript
{
get
{
return this.GetCallbackEventReference(this, "param", "handleResultFromServer", "context");
}
}
public string RaiseCallbackEvent(string eventArgument)
{
return "客户端在[" + DateTime.Now.ToString() + "]传送来 [" + eventArgument + "].";
}
}
We let the page implement the ICallbackEventHandler interface directly, and then the interface-defined RaiseCallbackEvent () method returns the server's time and the data coming back together.
The function of the ClientScript property is that it invokes the page's GetCallbackEventReference () method, obtains the JS script that enables the client to invoke the server-side method, and outputs it to the page's Calltoserver () method, so that When you click on the page button, you start executing the Calltoserver () method that contains the call server method on the page.
Note the parameters of the GetCallbackEventReference () method, in which we define which variable of the client contains the information to be passed to the server, which method of the client is invoked after the server method executes. For more information on getcallbackeventreference () see here.