Basic ajaxPro. dll tutorial (call the background method in the foreground and call the foreground method in the background)

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.
Download ajaxpro. dll from http://www.ajaxpro.info. The latest version is 6.4.15.1. The downloaded and decompressed folder contains AjaxPro. dll. Use VS2005 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>

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. We add a namespace such as MyAjaxNetTest to it, and then add this namespace in the first sentence of _ Default HTML:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "MyAjaxNetTest. _ Default" %>
Register this page in PageLoad to AjaxPro:
Protected void Page_Load (object sender, EventArgs e)
{
AjaxPro. Utility. RegisterTypeForAjax (typeof (_ Default ));
}

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 ();
}

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>

Then you can add an HTML button and set the onclick handler to getTime ().
<Input id = "Button1" type = "button" value = "button" onclick = "getTime ()"/>


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;
}

Method for adding GetStudent to the server:
[AjaxPro. AjaxMethod]
Public Student GetStudent ()
{
Return new Student ();
}

Correspondingly, the client adds a call:
Function getStudent ()
{
Var stu = MyAjaxNetTest. _ Default. GetStudent (). value;
Alert (stu. Name + "" + stu. Age );
}

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 student = null;
2 [AjaxPro. AjaxMethod]
3 public void SetStudent (Student stu)
4 {
5 this. student = stu;
6 string name = this. student. Name;
7}
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 );
}
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;
}
}
}
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

 


From the deepwishly Column

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.