asp.net 2.0, no refresh page new realm
--davey--
"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:
Result:
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 "The client is sent [" + Eventargument + "] on [" + DateTime.Now.ToString () + "].";
}
}
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