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:
- <Httphandlers>
- <Add verb = "post, get" Path = "ajaxpro/*. ashx" type = "ajaxpro. ajaxhandlerfactory, ajaxpro"/>
- </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,
- <% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "myajaxnettest. _ default" %>
Copy code
Register this page in pageload to ajaxpro:
- Protected void page_load (Object sender, eventargs E)
- {
- Ajaxpro. Utility. registertypeforajax (typeof (_ default ));
- }
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:
- [Ajaxpro. ajaxmethod]
- Public String getservertime ()
- {
- Return datetime. Now. tostring ();
- }
Copy code
The customer can now call this method in JS, as shown in figure
- <SCRIPT type = "text/JavaScript">
- Function gettime ()
- {
- Alert (myajaxnettest. _ default. getservertime (). value );
- }
- </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:
- Public class student
- {
- Public string name = "sky ";
- Public int age = 26;
- }
Copy code
Method for adding getstudent to the server:
- [Ajaxpro. ajaxmethod]
- Public student getstudent ()
- {
- Return new student ();
- }
Copy code
Correspondingly, the client adds a call:
- Function getstudent ()
- {
- VaR Stu = myajaxnettest. _ default. getstudent (). value;
- Alert (STU. Name + "" + Stu. Age );
- }
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:
- Private student = NULL;
- [Ajaxpro. ajaxmethod]
- Public void setstudent (student Stu)
- {
- This. Student = Stu;
- String name = This. Student. Name;
- }
Copy code
You can add a breakpoint on the sixth line. When the client calls the breakpoint, it will enter.
Client add call:
- Function putstudent ()
- {
- VaR Stu = myajaxnettest. _ default. getstudent (). value;
- Stu. Name = "chenqi ";
- Myajaxnettest. _ default. setstudent (Stu );
- }
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:
- Public class student
- {
- Private string name = "sky ";
- Public int age = 26;
- Public string name
- {
- Get
- {
- Return this. Name;
- }
- Set
- {
- This. Name = value;
- }
- }
- }
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:
- Function getvalue ()
- {
- Myajaxnettest. _ default. getvalue (getgroups_callback );
- }
- Function getgroups_callback (response)
- {
- VaR dt = response. value;
- Alert (DT );
- }
Copy code