Summary of the commonly used callback Processing Methods of anthem. Net [conversion]
From: http://www.cnblogs.com/RChen/archive/2006/09/12/anthem_callback.html
In anthem. Net, a refreshing call (usually asynchronous mode) made to the server through the XMLHTTP or XMLHttpRequest component is called a callback ).
This article describes how to sort out and summarize the sample code provided by the anthem. NET Framework. It focuses on the calling process control related content involved in 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 article to make 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="button1_PreCallBack" PostCallBackFunction="button1_PostCallBack" CallBackCancelledFunction="button1_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 cancelled!');
}
</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 );
}
Client:
<input id="a" size="3" value="1">
<input id="b" size="3" value="2">
<button onclick="DoAdd(); return false;" type="button">Add</button>
<input id="c" size="6">
// 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;
}
);
After the call, the result variable obtained in the callback function parameters 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.