Example from: ASP. NET 2.0 programming Pearl
Server:
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
// Inherit icallbackeventhandler to ensure that the callback event will be handled when the customer callback feature is used
Public partial class chapter4_demo4: system. Web. UI. Page, system. Web. UI. icallbackeventhandler
{
Protected void page_load (Object sender, eventargs E)
{
// Check whether your browser supports callback.
If (! Request. browser. supportscallback)
{
Throw new applicationexception ("This browser doesn't support client Callbacks .");
}
// Obtain the callback event reference
String src = page. clientscript. getcallbackeventreference (this, "Arg", "clientcallback", "CTX", "clienterrorcallback", false );
String mainsrc = @ "function validatenumber (ARG, CTX)
{"+ SRC + ";}";
Page. clientscript. registerclientscriptblock (this. GetType (), "validatenumber", mainsrc, true );
}
Private string _ returnformserver;
// Eventargs indicates the data in the client Method
Public void raisecallbackevent (string eventargs)
{
Try
{
Int value = int32.parse (eventargs );
If (value> = 1 & value <= 1000)
{
This. _ returnformserver = "you entered the number:" + eventargs;
}
Else
{
This. _ returnformserver = "Please enter a number between 1 and 1000 ";
}
}
Catch
{
Throw new applicationexception ("You must enter a number .");
}
}
// Return a string to the client. The return value is the callback method sent to the client as a parameter.
Public String getcallbackresult ()
{
Return this. _ returnformserver;
}
}
Client:
<HTML>
<Head runat = "server">
<Title> untitled page </title>
<Script language = JavaScript>
Function validate ()
{
VaR n = document.forms%0%.txt number. value;
Validatenumber (n, "txtnumber ");
}
Function clientcallback (result, context)
{
Alert (result );
Alert (context );
}
Function clienterrorcallback (error, context)
{
Alert ("the validation failed." + error );
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
Please enter a number between 1 and 1000: <br/>
<Input id = "txtnumber" name = "txtnumber" type = text/>
<Button id = "btnvalidate" onclick = "Validate ()"> validate </button>
</Div>
</Form>
</Body>
</Html>
Author: Ice dish
Source: http://www.cnblogs.com/icebutterfly/
Copyright: The copyright of this article is shared by the author and the blog
Reprinted: you are welcome to reprinted. To save the author's Creative Enthusiasm, please [reprinted] As required. Thank you.
Requirement: This statement must be retained without the author's consent; Article The original text connection is provided; otherwise, the legal liability is required.