Icallbackeventhandler: Implementation of the callback function (Memo)

Source: Internet
Author: User

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.

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.