A self-implemented script callback

Source: Internet
Author: User

A friend on the Internet recommended me to read an article yesterday.ArticleRemote scripting in. net, the author of this article uses the Microsoft Remote scripting technology to encapsulate some files on the server to facilitate ASP.. net.

Speaking of this, I used to be quite unfamiliar with Microsoft Remote scripting. The principle of Mrs is to use a built-in Java Applet through some high-level encapsulation, this allows scripts on the page to directly call methods published on the server. If you are interested, you can take a look at the documentation on msdn. It is convenient to use it, especially the encapsulated. NET component developed by the author of the previous article.

However, I personally don't catch a cold with applet, especially on the WINXP sp2rc2 machine that didn't install JRE in my house...

In fact, my favorite is ASP. the implementation method of script callback in net 2.0 is simple and clear, so I think that since it is also implemented using XMLHTTP, it is better for me to implement it in ASP. NET 1.1 is also implemented with XMLHTTP. It took a while to complete. Most of them are counterfeit ASP. NET 2.0 implementation method, but in some places it cannot be completely the same, because for the processing of controls that implement the icallbackeventhandler interface, I cannot directly write to the page class or more, therefore, you must create a scriptcallbackmanager control on the page.

Steps for the server:
1. Let a widget on the page implement the icallbackeventhandler interface. I personally like it and let the page class implement it directly;
2. Implement the raisecallbackevent method on the control that implements the icallbackeventhandler interface to process client requests;
3. Add a scriptcallbackmanager control to the page, specify the control that implements the icallbackeventhandler interface in the constructor, and the script function name of the client after the call is completed.

Public class webform1: system. Web. UI. Page, icallbackeventhandler
{
Private void page_load (Object sender, system. eventargs E)
{
This. Controls. Add (New scriptcallbackmanager (this, "handleresultfromserver "));
}

Public String raisecallbackevent (string eventargument)
{
Return "you sent" + eventargument + "! ";
}

Steps on the page:
1. directly call the built-in scriptcallback () function where the server method needs to be called. The first parameter specifies the variables to be passed to the server, and the second parameter specifies the context of the request;
2. Compile a function that is automatically executed after the call is completed. The name of the function must be the same as the name specified in the constructor of the scriptcallbackmanager control on the server.

Function callback ()
{
VaR Param = document.all.txt request. value;
VaR context = "";

Scriptcallback (Param, context );
}

Function handleresultfromserver (result, context)
{
Alert (result );
}

In the callback () method, a built-in Script Function scriptcallback () is called. This function automatically calls the server method and sends the value of the first parameter to the raisecallbackevent () of the server () method parameters. Handleresultfromserver is a function automatically executed after the call is complete.

If you are interested, you can download the project source code of this script callback implementation.

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.