Download and use ajaxpro. dll

Source: Internet
Author: User

The rise of Ajax technology has also increased the expressiveness of B/S applications and gradually swallowed up the territory of C/S. With ajaxpro. dll, you can call the. NET method from a javascript client.
First download ajaxpro. dll. The downloaded and decompressed folder contains ajaxpro. dll. Use vs to create a web project, add a reference to ajaxpro. dll, and add the following in the Web configuration file:

  1. <Httphandlers>
  2. <Add verb = "post, get" Path = "ajaxpro/*. ashx" type = "ajaxpro. ajaxhandlerfactory, ajaxpro"/>
  3. </Httphandlers>

Copy code

This configuration item indicates all the ajaxpro /*. all ashx requests (Ajax requests sent from the customer) are sent to ajaxpro. ajaxhandlerfactory, instead of the default system. web. UI. pagehandlerfactory.
The new web project has a default _ default page,

  1. <% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "myajaxnettest. _ default" %>

Copy code

Register this page in pageload to ajaxpro:

  1. Protected void page_load (Object sender, eventargs E)
  2. {
  3. Ajaxpro. Utility. registertypeforajax (typeof (_ default ));
  4. }

Copy code

Test 1:
Everything is ready. Let's first test the simple method from the client to call the server. First, add the method to the _ default class:

  1. [Ajaxpro. ajaxmethod]
  2. Public String getservertime ()
  3. {
  4. Return datetime. Now. tostring ();
  5. }

Copy code

The customer can now call this method in JS, as shown in figure

  1. <SCRIPT type = "text/JavaScript">
  2. Function gettime ()
  3. {
  4. Alert (myajaxnettest. _ default. getservertime (). value );
  5. }
  6. </SCRIPT>

Copy code

Test 2:
Add static modifier to the getservertime method. The test is still successful!

Test 3:
The call to a simple method is okay. The getservertime method returns a simple string. Can the server return a slightly more complex object? Let's try it. Create a new student class:

  1. Public class student
  2. {
  3. Public string name = "sky ";
  4. Public int age = 26;
  5. }

Copy code

Method for adding getstudent to the server:

  1. [Ajaxpro. ajaxmethod]
  2. Public student getstudent ()
  3. {
  4. Return new student ();
  5. }

Copy code

Correspondingly, the client adds a call:

  1. Function getstudent ()
  2. {
  3. VaR Stu = myajaxnettest. _ default. getstudent (). value;
  4. Alert (STU. Name + "" + Stu. Age );
  5. }

Copy code

Follow the preceding HTML button to test the getstudent function. The answer is: as we expected, client JS can access the objects returned by the server.

Test 4:
Finally, let's take a look at the ability to submit objects to the server on the client. Add the method on the server first:

  1. Private student = NULL;
  2. [Ajaxpro. ajaxmethod]
  3. Public void setstudent (student Stu)
  4. {
  5. This. Student = Stu;
  6. String name = This. Student. Name;
  7. }

Copy code

You can add a breakpoint on the sixth line. When the client calls the breakpoint, it will enter.

Client add call:

  1. Function putstudent ()
  2. {
  3. VaR Stu = myajaxnettest. _ default. getstudent (). value;
  4. Stu. Name = "chenqi ";
  5. Myajaxnettest. _ default. setstudent (Stu );
  6. }

Copy code

Similarly, when the putstudent js method is called, the server enters the breakpoint, indicating that the customer has successfully submitted the object, and the Object Name field has changed to "chenqi.

Test 5:
All the public fields set by the customer are student. What is the access attribute? The student definition is changed as follows:

  1. Public class student
  2. {
  3. Private string name = "sky ";
  4. Public int age = 26;
  5. Public string name
  6. {
  7. Get
  8. {
  9. Return this. Name;
  10. }
  11. Set
  12. {
  13. This. Name = value;
  14. }
  15. }
  16. }

Copy code

Repeat the previous test and I think it's already in your expectation.

From the previous small tests, I have discovered the convenience and speed of using ajaxpro. dll. It seems that B/S development is no longer as tedious as I previously felt.
Asynchronous call:

  1. Function getvalue ()
  2. {
  3. Myajaxnettest. _ default. getvalue (getgroups_callback );
  4. }
  5. Function getgroups_callback (response)
  6. {
  7. VaR dt = response. value;
  8. Alert (DT );
  9. }

Copy code

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.