Using the Ajaxpro.net framework to implement methods to invoke the server side on the client

Source: Internet
Author: User

This document will implement AJAX functionality using the Ajaxpro.net framework: calling the server-side method asynchronously at the client. Ajaxpro.net is an excellent. NET Environment Ajax framework, the use is very simple, you can access the relevant information, this document is a simple example of the use of Ajaxpro several key points.

1, download Ajaxpro components. and refer the AjaxPro.dll to the Web site (or project). Download: Download Latest Version 7.7.31.1.
2, modify web.config. Add the following code to the <system.web> element.
<configuration><system.web> <add verb= "Post,get" path= "Ajaxpro/*.ashx" type= "ajaxpro.ajaxhandlerfactory, ajaxpro.2"/>
3, the Ajaxpro in the page Page_Load event in the Run-time registration. Such as:
AjaxPro.Utility.RegisterTypeForAjax (typeof (class name) of the class); Class name of the class. If you put it in a namespace, you need to write a complete namespace (such as: Namespaces._default)
AjaxPro.Utility.RegisterTypeForAjax (typeof (TestPro1));
4, create the server-side method. As long as you add a [Ajaxpro.ajaxmethod] tag to a method, the method becomes a Ajaxpro method that can be invoked by innuendo. as follows: (I am now a new testpro1.aspx page, added in its CS code)
Copy CodeThe code is as follows:
[Ajaxpro.ajaxmethod]
public string GetString ()
{
Return "Hello Ajaxpro";
}
[Ajaxpro.ajaxmethod]
public string GetServerTime ()
{
return DateTime.Now.ToString ();
}

5, the client calls:
Copy CodeThe code is as follows:
<script type= "Text/javascript" >
function GetTime () {
Alert (Testpro1.getservertime (). value);
}
function Getserverstr () {
Ajaxpro_guide. GetString (Getstring_callback); Asynchronous call
var p = classpro.getservertime (). toString ();
Alert (testpro1.getstring (). value);
}
</script>

Add the following code to the page:
<input id= "Button1" type= "button" value= "is Server Time"/>
<input id= "Button3" type= "button" value= "is a server object"/>

Second, extended, Client Access server object
1. Create a new class in App_Code:
Copy CodeThe code is as follows:
public class Student
{
private string _name = "Cheng";
public int age = 30;
public string Name
{
get {return this._name;}
set {this._name = value;}
}
}

2, in the test page testpro1.aspx page, in its CS code to join
Copy CodeThe code is as follows:
[Ajaxpro.ajaxmethod]
Public Student getstudent ()
{//server-side Add Getstudent method
return new Student ();
}
Private Student Student = null;
[Ajaxpro.ajaxmethod]
public void Setstudent (Student stu)
{
This.student = Stu;
String name = This.student.Name;
}

3. JavaScript script for ASPX pages
Test the script in the ASPX page
Copy CodeThe code is as follows:
<title>ajaxpro Test </title>
<script type= "Text/javascript" >
function Getstudent () {
var stu = Testpro1.getstudent (). value;
Alert (Stu. Name + "" + stu. Age); Client JS can access the objects returned by the server
}
function Putstudent () {
var stu = Testpro1.getstudent (). value;
Stu. Name = "Liu";
Testpro1.setstudent (Stu); The client submits the object, and the Name field of the object has changed to "Liu".
Alert (Stu. Name + "" + stu. Age); Client JS can access the objects returned by the server
}
</script>

<div><input id= "Button3" type= "button" value= "is a server object"/>
<input id= "Button4" type= "button" value= "Client commit object to server"/>
</div>
Reference: Official website



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.