Callback Technology of callback in. net

Source: Internet
Author: User
At the beginning of. net2.0, a new interface icallbackeventhandler is provided to indicate that the control can be used as the target interface of the server's callback event. The method provided by this interface can also achieve partial page update.
Icallbackeventhandler mainly provides two methods:
(1) string getcallbackresult ();
No parameter. The returned data type is string type, and the returned content is the result of callback event execution.
(2) void raisecallbackevent (string eventargment );
No return value. The parameter type is string type. This parameter is generally used for data parameters transmitted between the client and the server.
If a control requires a local callback function, it must implement the icallbackeventhandler interface. To obtain the results returned by the server getcallbackresult on the client, you must call
Public String getcallbackeventreference (Control, string argument, string clientcallback, string context)
In this method, control indicates the server control that processes the client callback, that is, the control that implements the icallbackeventhandler interface. Argument is the parameter passed from the client to the server method void raisecallbackevent (string eventargment. Clientcallback is handled by the customer service. Program The name of the string returned by the getcallbackresult method processed by the program. Context, the client script calculated on the client before the callback is started, also known as the script context.
Below is a simple example. Code It is very simple, not to mention too much.
(1) client code Code
<% @ Page Language = " C # " Autoeventwireup = " True " Codefile = " Default2.aspx. CS " Inherits = " Default2 "   %>

<!Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

< Html Xmlns = "Http://www.w3.org/1999/xhtml" >
< Head Runat = "Server" >
< Title > No title page </ Title >
< Script Type = "Text/JavaScript" >
Function Callbackdo ()
{
VaR Resultstr = Specified parameter Doc ument. getelementbyid ( " Text1 " ). Value;
<% = This . Clientscript. getcallbackeventreference ( This , " Resultstr " , " Getresult " , Null ) %>
}
Function Getresult (datastr)
{
Document. getelementbyid ( " Mytext " ). Innerhtml = Datastr;
}
</ Script >
</ Head >
< Body >
< Form ID = "Form1" Runat = "Server" >
< Div >
< Div >
< Input ID = "Text1" Type = "Text"   /> </ Div >
< Div > < Input ID = "Button1" Type = "Button" Value = "Button" Onclick = "Callbackdo ()" /> </ Div >
< Div ID = "Mytext" > </ Div >
</ Div >
</ Form >
</ Body >
</ Html >

(2) server code Code
Public   Partial   Class Default2: system. Web. UI. Page, icallbackeventhandler
{
String Resultstr =   "" ;
Protected   Void Page_load ( Object Sender, eventargs E)
{

}

Public void raisecallbackevent ( string eventargument)
{< br> resultstr = " after being processed by the server, " + eventargument;
}

Public StringGetcallbackresult ()
{
ReturnResultstr;
}
}

(3) Test Results
Enter "" in the input box, and click the button. The page displays "after being processed by the server"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.