Summary of common callback processing methods using anthem. net

Source: Internet
Author: User

In anthem. Net, a refreshing call (usually asynchronous mode) made to the server through the XMLHTTP or XMLHttpRequest component is called a callback ).

The content of this article is an example of the anthem. NET Framework.CodeSummary of the calling process control related to Ajax development using anthem. net. As for the use of the control, the logic is simple and is not described here.

After this article, I plan to write an articleArticleMake a complete induction of the call Process and Its Control Points during programming.

1. Common calls

<% @ Register tagprefix = "Anthem" namespace = "Anthem" assembly = "Anthem" %>
<Anthem: button id = "button" runat = "server" text = "Click me! "/>
<Anthem: Label id = "label" runat = "server"/>
<SCRIPT runat = "server">
Void button_click (Object sender, eventargs e ){
Label. Text = datetime. Now. tostring ();
Label. updateaftercallback = true;
}
</SCRIPT>

 

2. Add the execution logic of user-defined client functions before and after callback

Several common attributes:

Precallbackfunction: used to define the function executed before the callback. Generally, you can add a confirmation judgment here.
In this function, return false will cancel the callback.

Postcallbackfunction: The function executed after callback.
Textduringcallback: used to define the prompt information displayed by the control during the callback process (usually the prompt waiting text)
Enabledduringcallback: whether the control is disabled during the callback process.
Callbackcancelledfunction: This function is called if the callback is canceled.

Code example:

<Anthem: button id = "button1" runat = "server" text = "Click me! "Textduringcallback =" working... "enabledduringcallback =" false "precallbackfunction =" button?precallback "postcallbackfunction =" button=postcallback "callbackcancelledfunction =" button#callbackcancelled "/>

<Script language = "JavaScript" type = "text/JavaScript">
// Before callback, you can cancel the callback here
Function button1_precallback (button ){
If (! Confirm ('Are you sure you want to perform this call back? ')){
Return false;
}
Document. getelementbyid ('button2'). Disabled = true;
}

// After callback is complete
Function button1_postcallback (button ){
Document. getelementbyid ('button2'). Disabled = false;
}

// After canceling the callback
Function button1_callbackcancelled (button ){
Alert ('Your call back was canceled! ');
}

</SCRIPT>

Note that the above client processing functions can pass the control itself as the parameter, so these functions can be reused if necessary. (For example, handling the sending events of a group of similar controls)

Iii. Method of calling the Server Page

What the server needs to do:

[Anthem. Method]
Public int add (int A, int B ){
Return A + B;
}

void page_load () {
anthem. manager. register (this);
}< br>
client:


Add


// The meanings of parameters are as follows:
// name of the server method,
// method parameters (transmitted as JS arrays ),
// The callback function called when the server method returns the result (because it is in asynchronous mode ).
anthem_invokepagemethod (
'add',
[document. getelementbyid ('A '). value, document. getelementbyid ('B '). value],
function (result) {
document. getelementbyid ('C '). value = result. value;
}< br>);

the result variable obtained in the callback function parameters after the call, is an object that contains two attributes: Value and error. If an error occurs on the server, the value is null, and the error contains the error data.

4. How to handle possible exceptions during callback

Define the anthem_error JS function on the page to handle unhandled exceptions during all callbacks.

<SCRIPT type = "text/JavaScript">
Function anthem_error (result ){
Alert ('anthem _ error was invoked with the following error message: '+
Result. Error );
}
</SCRIPT>

Exceptions can also be handled on the server side. You only need to define the following method:

Void page_error ()
{
Anthem. Manager. addscriptforclientsideeval ("window. Location = 'HTTP: // anthem-dot-net.sf.net /'");
}

Processing on the server has some additional benefits, mainly because the exception information can be recorded in logs.

V. page Jump

In the process of callback, you cannot use response. Redirect to process page redirects, because these functions are called through JS without refreshing. The alternative is to use anthem. the manager returns a piece of js code to the client for eval execution, so as to achieve the page Jump effect (by extension, this eval function is of course not limited to page Jump, you can do a lot of other things ).

Sample Code:

Anthem. Manager. addscriptforclientsideeval ("window. Location = 'HTTP: // anthem-dot-net.sourceforge.net /';");

6. Global client callback Functions

We can define several special name functions on the client for Anthony to call each callback. These functions include:
Anthem_precallback (),
Anthem_callbackcancelled (),
Anthem_postcallback (),

In addition, it also includes the mentioned anthem_error.

A typical application scenario here is that, after each callback starts, we create a "loading" information layer in anthem_precallback (), and then cancel (anthem_callbackcancelled) or remove this layer after successful callback (anthem_postcallback.
This allows you to easily simulate loading effects similar to those in Gmail.

7. A new JS script is added to the page during the callback process.

In this case, an additional attribute must be set:
Anthem. Manager. includepagescripts = true;

Example:

<SCRIPT runat = "server">

Protected void button#click (Object sender, eventargs E)
{
Htmlbutton button = new htmlbutton ();
Button. innertext = "Now click me! "
Button. attributes ["onclick"] = "thankyou ();"
Placeholder1.controls. Add (button );
Placeholder1.updateaftercallback = true;
String script = @ "<SCRIPT type =" "text/JavaScript" ">
Function thankyou (){
Alert ('thank you! ');
}
</"+" Script>"
# If V2
Page. clientscript. registerclientscriptblock (typeof (PAGE), script, script );
# Else
Page. registerclientscriptblock (script, script );
# Endif
Anthem. Manager. includepagescripts = true;
}
</SCRIPT>

8. preupdate events

If the value of updateaftercallback is true before render, this event is triggered.
Currently, this event does not seem to be useful.

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.