(About Ajax)

Source: Internet
Author: User

I will not reference any original article on the Internet in this article. It is my own experience. I believe it will help many people who want to know what Ajax is and how it is applied, because I am also a beginner, so there may be something wrong with the writing. I hope the old programmers can point out that we should never miss our children. This is another purpose for me to write this article.
Note: I use ASP. NET
I don't want to talk about Ajax's long-length theory. I just want to know about the full name, Dom, XMLHTTP, and other online materials.
[Ajax-free pages are not called Web2.0. Ajax is used for Web pages.]
The purpose of AJAX is nothing more than to update a page without refreshing the page. The so-called asynchronous mode means that no background program is executed, and the foreground will get the corresponding change value, so as to change data on the foreground page, the changed value is changed through the subsequent CS code operation. It sounds quite uncomfortable. How did this happen? Let me talk about it.
After my research over the past few days, there are more than one method to implement Ajax in ASP. NET.
1. some frameworks are used directly: I like this one, because it is clean and I have tried ajaxpro and prototype with my own framework. I have used these two frameworks and I think they are good, I will continue to use jquery in the future. jquery actually belongs to this category. I have been studying it for a long time and it is not very clear. Maybe I have not found a ready-made framework, so I will not study it.
2. there is also a control package specially developed for Ajax by Microsoft, called ASP. net Ajax Control Toolkit. I have not studied it too much. I have related information on the Internet. If you want to know it, you can check it by yourself. There are many ready-made controls that can implement Ajax.
3. With vs2005, you can use the next aspajaxextsetup. MSI, which contains the updatepanel control to implement Ajax. I just tried it and it is also easy to use, but the principle is not clear yet.
The above three methods are some of the Ajax implementation methods I have collected. I want to talk about the application of the first method, because the first method can help you understand the running principle of Ajax and is suitable for beginners.
I mainly talk about ajaxpro, which is a component and must be downloaded online. It is actually an ajaxpro. DLL files. Note that there are two types of files called Ajax on the Internet. DLL is called ajaxpro. DLL, the two functions are the same, that is, the statements for adding references are somewhat different. I will explain them separately below. In fact, Ajax requires related components. At first, I am not used to any plug-ins, but later I went down. I went down to Ajax. dll, so Ajax. dll prevails.
Application. The following articles will be used by other people on the Internet.
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 code to the <system. Web> element. 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 ajaxpor. 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?

The above is my understanding of Ajax, and I do not know whether it is true or not. However, I am also confident. If it is not true, please point it out to me.

Related Article

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.