CallBack Technology in Asp.net Programming)

Source: Internet
Author: User

CallBack Technology)
In asp.net, the interaction between the client and the server is submitted on the whole page by default. At this time, the client submits the data in the current page form (including some automatically generated hidden fields) to the server, the server re-instantiates an instance of the current page class to respond to this request and then resends the content of the entire page to the client. This processing method has no effect on the running results, however, this method increases the network's data transmission burden, increases the server's work pressure, and users still need to wait for the final processing results. If we want to have such a function, after the user fills in the user name, we will check whether the user name already exists in the server database. If so, we will prompt that the user name already exists, if the user name does not exist, it is prompted that the user name is available. In this case, you only need to pass a user name as the parameter. However, you need to submit the entire form. There are currently three mainstream solutions to the above problems: Pure javascript implementation, Microsoft Ajax class library implementation, and AjaxPro implementation. The next two practices will be discussed in a later article. Here I will talk about another implementation: Using callback technology.

Creating a webpage that implements callback technology is similar to a common asp.net webpage, but the following special work is required:
(1) Implement the ICallbackEventHandler interface on the current page. This interface defines two methods: the string GetCallbackResult () method and the void RaiseCallbackEvent (string eventArgument) method. The GetCallbackResult () method is used to return the result of a callback event with the control as the target. The RaiseCallbackEvent () method is used to process callback events with the control as the target.
(2) provide three javascript client script functions for the current page. A javascript function is used to execute actual requests to the server. In this function, a string type parameter can be provided to the server; another javascript function is used to receive the string type result returned after the execution of the server method and process the result. Another is to execute the help function for the server request, through GetCallbackEventReference () in the server code () asp.net automatically generates this function when obtaining the reference of this method.
The following is a detailed example of how to use callback to create a Register. aspx page using Dreamweaver. The Code is as follows:

 

<% @ Page Language = "C #" ContentType = "text/html" ResponseEncoding = "gb2312" %>
<% @ Implements Interface = "System. Web. UI. ICallbackEventHandler" %>
<% @ Import Namespace = "System. Text" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> User Registration </title>
<Script language = "javascript">
// Method executed by the client
// The following method receives and processes the returned results of server method execution.
Function Success (args, context)
{
Message. innerText = args;
}
// The following method is called when an exception occurs in the processing result of the receiving server method.
Function Error (args, context)
{
Message. innerText = 'exception occurred ';
}
</Script>
<Script language = "c #" runat = "server">
String result = "";
// Define the callback method running on the server.
Public void RaiseCallbackEvent (String eventArgument)
{
If (eventArgument. ToLower (). IndexOf ("admin ")! =-1)
{
Result = eventArgument + "cannot be registered as a user name. ";
}
Else
{
Result = eventArgument + "can be registered. ";
}
// Throw new Exception ();
}
// Define the method for returning the callback method execution result
Public string GetCallbackResult ()
{
Return result;
}
// Method executed on the server
Public void Page_Load (Object sender, EventArgs e)
{
// Obtain the reference of ClientScriptManager on the current page
ClientScriptManager csm = Page. ClientScript;
// Obtain the callback reference. The WebForm_DoCallback method is generated on the client to call it asynchronously. This method is written by Microsoft and will be sent to the client.
// Note that the "Success" and "Error" strings here are the two javascript Functions Defined in the client code respectively.
// The meaning of the last parameter of the following method: true indicates that asynchronous callback is executed, and false indicates that synchronous callback is executed.
String reference = csm. GetCallbackEventReference (this, "args", "Success", "", "Error", false );
String callbackScript = "function CallServerMethod (args, context) {\ n" +
Reference + "; \ n }";
// Register javascript script code with the current page
Csm. RegisterClientScriptBlock (this. GetType (), "CallServerMethod ",
CallbackScript, true );
}
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Table border = "1" cellpadding = "0" cellspacing = "0" width = "400px">
<Tr>
<Td width = "100px"> User Name </td> <input type = "text" size = "10" maxlength = "20" id = "txtUserName" onblur = "CallServerMethod (txtUserName. value, null) "/> <span id =" message "> </span> </td>
</Tr>
<Tr>
<Td> password </td> <input type = "password" size = "10" maxlength = "20" id = "txtPwd"/> </td>
</Tr>
</Table>
</Form>
</Body>
</Html>
I have added enough detail to watch the above page, but I still need to explain the following points:
(1)

<% @ Implements Interface = "System. Web. UI. ICallbackEventHandler" %>
This statement indicates that the current page implements the ICallbackEventHandler interface. If the page is separated from the code, the background cs code should be:

Public partial class Register: System. Web. UI. Page, ICallbackEventHandler
{
// Cs code
}
(2)

<Input type = "text" size = "10" maxlength = "20" id = "txtUserName" onblur = "CallServerMethod (txtUserName. value, null)"/>
Here is an onblur = "CallServerMethod (txtUserName. value, null), which indicates that the CallServerMethod client method is triggered when the user name text box loses focus. This client method is dynamically generated by asp.net.
(3)

Csm. GetCallbackEventReference (this, "args", "Success", "", "Error", false );
"Success" and "Error" represent the javascript Functions of the client, which can be seen in the Code. "Success" represents the name of the client method to be executed after the server method is successfully called, "Error" indicates the name of the client method called when the server method fails to be called.

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/zhoufoxcn/archive/2008/09/28/2990318.aspx

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.