Ajax. dll and ajaxpro. dll

Source: Internet
Author: User

Application, the following things will be used by others on the InternetArticle, Declare it here

1. first put Ajax. DLL is added to the project. Don't tell me I won't. If this is the case, right-click the project and choose [add reference] from the menu. then, step by step. after adding the DLL file, you will see the Ajax in the reference of the project. DLL is successfully added.
2. modify web. config. Add the following to the <system. Web> element:Code. The Ajax. dll and ajaxpro. dll reference methods here are different. Be sure to pay attention to them.
<Configuration>
<System. Web>
<Httphandlers>
<! -- The configuration file of Ajax. dll is written as, which is the one I downloaded -->
<Add verb = "post, get" Path = "ajax/*. ashx" type = "Ajax. pagehandlerfactory, Ajax"/>
<! -- The configuration file of ajaxpro. dll is written as: select different configuration statements based on the DLL file you downloaded -->
<Add verb = "*" Path = "ajaxpro/*. ashx" type = "ajaxpro. ajaxhandlerfactory, ajaxpro"/>
</Httphandlers>
</System. Web>
</Configuration>

3. register the page page_load event used by ajaxpro during runtime. For example:
Protected void page_load (Object sender, eventargs E)
{
Ajax. Utility. registertypeforajax (typeof (_ default); // Ajax. dll
Ajaxpro. Utility. registertypeforajax (typeof (_ default); // ajaxpro. dll
}
// This _ default refers to the Class Name of the page class, which is the name of the page. If it is placed in a namespace, you must enter the complete namespace (for example, namespaces. _ default)

4. Create a server
[Ajax. ajaxmethod] // This sentence must exist. If you are ajaxpro. dll, write it as [ajaxpro. ajaxmethod].
Public String getvalue (int A, int B)
{
// This method transfers two numbers from the client and returns them to the client after adding them to the server. You can write a class in the background of the original page.
Return convert. tostring (a + B); // The value returned here is the value obtained at the front end. The parameter is already included in the CS file. You can perform the operation as needed, including reading the database.
}
5. Client call.
<% @ Page Language = "C #" codebehind = "webpage1.aspx. cs" autoeventwireup = "false" inherits = "Web. webpage1" %>
<Script language = "JavaScript">
Function getvalue ()
{

// If it is ajaxpro. dll, add web. _ default. getvalue. If it is Ajax. dll, you do not need to add the following namespace:
_ Default. getvalue (, getgroups_callback); // call the _ default. getvalue method on the server.
// _ Default is the class that writes getvalue. If it is written in Cs on this page, webpage1.getvalue, 1 and 2 are parameters.
// Getgroups_callback specifies a callback function to receive the client result after the server completes processing.
}

// The user accepts and processes the results returned by the server.
Function getgroups_callback (response)
{
VaR dt = response. value; // The value is the value that is finally passed back. Use it whenever you want. Return to the front-end anyway.
Document. getelementbyid ("div_1"). innerhtml = DT;
}
</SCRIPT>
<Body>
<Div id = "div_1"> </div>
<Button onclick = getvalue ()> Start </botton>
</Body>

Now let's analyze what is executed by clicking the start button. First, execute the getvalue () function on the foreground, and the _ default function in the getvalue function. getvalue (, getgroups_callback); will execute the CS function in the background. I think this is the essence of Ajax, because the execution here is based on the Ajax component without refreshing the execution of the CS function in the background, generally, we need to call the CS function in the background to refresh the page through the normal method and execute the CS function bound in the background. Here we use ajax to execute the getvalue function in the background without refreshing, 1 and 2 are parameters, which need to be calculated in getvalue. The getgroups_callback parameter must be included. Otherwise, what kind of reception do you use to transmit things back in CS, after the getvalue function calculates the result in the background, the calculation is already in the background. If you want to calculate the result, it's okay to read the database and then return the value through return, this value can be anything, even if it is an HTML code like "<Table> <tr> <TD> helloworld </TD> </tr> </table>, the front-end uses the getgroups_callback () JS function to receive this value, and then the front-end calls it. If you want to use it, you can use it as needed. This is a process of running Ajax, from the front-end to the back-end, and then back to the front-end after computation. How do you understand?

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/chinaccd/archive/2009/05/14/4182574.aspx

 

Of course, we do not want to use this powerful capability to warn users. This is why all client proxies (such as the Javascript sample. serversidead function) accept other features. This feature is the callback function called to handle the response:
Sample. serversideadd (100,99, serversideadd_callback );

Function serversideadd_callback (response ){
If (response. Error! = NULL ){
Alert (response. Error );
Return;
}
Alert (response. value );
}

 

From the code above, we can see that another parameter is specified. Serversideadd_callback (see the preceding Code) is a client function used to process server responses. This callback function receives a response object that exposes three main properties.
Value -- the value actually returned by the server-side function (whether it is a string, a custom object, or a dataset ).
Error -- error message, if any.
Request -- XML: the original response of the HTTP request.
Context -- Context object.
First, check the error to see if an error has occurred. By throwing an exception in a server-side function, you can easily handle the error feature. In this simplified example, use this value to warn the user. The request feature can be used for more information (see the next section ).
Processing type
Return complex types
The Ajax Wrapper can not only process integers returned by the serversideadd function. It also supports integers, strings, double, booleans, datetime, datasets, datatables, custom classes, arrays, and other basic types. All other types return their tostring values.
The returned datasets is similar to the real. Net dataset. Suppose a server-side function returns dataset. We can use the following code to display the content on the client:

<Script language = "JavaScript">
// Asynchronous call to the mythical "getdataset" server-side function
Function getdataset (){
Ajaxfunctions. getdataset (getdataset_callback );
}
Function getdataset_callback (response ){
VaR DS = response. value;
If (Ds! = NULL & typeof (DS) = "object" & Ds. tables! = NULL ){
VaR S = new array ();
S [S. Length] = "<Table border = 1> ";
For (VAR I = 0; I <Ds. Tables [0]. Rows. length; I ++ ){
S [S. Length] = "<tr> ";
S [S. Length] = "<TD>" + Ds. Tables [0]. Rows [I]. firstname + "</TD> ";
S [S. Length] = "<TD>" + Ds. Tables [0]. Rows [I]. Birthday + "</TD> ";
S [S. Length] = "</tr> ";
}
S [S. Length] = "</table> ";
Tabledisplay. innerhtml = S. Join ("");
}
Else {
Alert ("error. [3001]" + response. Request. responsetext );
}
}
</SCRIPT>

AJAX can also return custom classes. The only requirement is that the serializable attribute must be used. Suppose there are the following classes:
[Serializable ()]
Public class user {
Private int _ userid;
Private string _ firstname;
Private string _ lastname;

Public int userid {
Get {return _ userid ;}
}
Public String firstname {
Get {return _ firstname ;}
}
Public String lastname {
Get {return _ lastname ;}
}
Public user (INT _ userid, string _ firstname, string _ lastname ){
This. _ userid = _ userid;
This. _ firstname = _ firstname;
This. _ lastname = _ lastname;
}
Public user (){}
[Ajaxmethod ()]
Public static user getuser (INT userid ){
// Replace this with a DB hit or something
Return new user (userid, "Michael", "Schwarz ");
}
}

You can register the getuser agent by calling registertypeforajax:
Private void page_load (Object sender, eventargs e ){
Utility. registertypeforajax (typeof (User ));
}

In this way, you can call getuser asynchronously on the client:

<Script language = "JavaScript">
Function getuser (userid ){
User. getuser (getuser_callback );
}
Function getuser_callback (response ){
If (response! = NULL & response. value! = NULL ){
VaR user = response. value;
If (typeof (User) = "object "){
Alert (user. firstname + "" + User. lastname );
}
}
}
Getuser (1 );
</SCRIPT>

 

The value returned in the response is actually an object that exposes the same attributes (firstname, lastname, and userid) as the server object ).
Custom Converter
We have seen that the Ajax. Net package can process many different. Net types. However, except for a large number of. Net classes and built-in types, the wrapper only calls tostring () for other types that cannot be correctly returned (). To avoid this, the Ajax. Net package allows developers to create an object converter for smoothly passing complex objects between the server and the client.
Other items
Register functions in other classes
In the above example, our server-side functions are all placed in the code behind the execution page. However, there is no reason not to place these functions in a separate class file. Remember, the way the wrapper works is to find all methods with Ajax. ajaxmethod in the specified class. The required class is specified through the Second Script tag. With Ajax. Utility. registertypeforajax, we can specify any classes required. For example, it is reasonable to use our server-side functions as separate classes:

Public class ajaxfunctions
<Ajax. ajaxmethod ()> _
Public Function validate (username as string, password as string) as Boolean
''Do something
''Return something
End Function
End Class

By specifying the type of the class rather than the page, you can let the Ajax package create a proxy:
Private void page_load (Object sender, eventargs e ){
Ajax. Utility. registertypeforajax (typeof (ajaxfunctions ));
//
}

Remember that the name of the client proxy is <classname>. <serversidefunctionname>. Therefore, if the serversideadd function is placed in the fictitious ajaxfunctions class above, the client call should be: ajaxfunctions. serversideadd (1, 2 ).

Returns Unicode characters.
The Ajax. Net package can return Unicode characters from the server to the client. Therefore, data must be HTML encoded on the server before being returned. For example:
[Ajax. ajaxmethod]
Public String test1 (string name, string email, string comment ){
String html = "";
HTML + = "hello" + name + "<br> ";
HTML + = "Thank you for your comment <B> ";
HTML + = system. Web. httputility. htmlencode (comment );
HTML + = "</B> .";
Return HTML;
}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.