AjaxPro使用方法
无意中看到同事用AjaxPro用,看到很不错,特长是前后台传输数据特别方便。
最好的教材就是拿到就可以用,方便大家。
以前数据传输用FORM提交,或者在前台用JASON拼接然后通过AJAX方式提交。总之要写好多代码,很不方便。要么页面刷新,要么总是写一件重复代码。
AjaxPro的方便之处就是前台不需要写太多的代码,可以直接跟后台方法进行访问,中间用数据JASON数据传输这一切她都已经帮你做了。网上找了几个教程都感觉不错。现在我把她从配置到例子整理下分享给大家。
一、配置AjaxPro:
1. 下载AjaxPro.2.dll并添加到工程里,如图:
2. 在web.config中添加注册信息
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Collections.Generic; namespace AjaxProTest { public partial class _Default : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { if (!Page.IsPostBack) { AjaxPro.Utility.RegisterTypeForAjax( typeof (_Default)); } } //return string [AjaxPro.AjaxMethod] public String GetAjaxString( string str) { return "Hello AjaxPro: " + str + Session.Count; } //return object. [AjaxPro.AjaxMethod] public BehaviourInfo GetAjaxObject() { var behaviourInfo = new BehaviourInfo(); behaviourInfo.Behaviour = "this is test behaviour..." ; return behaviourInfo; } //return datatable. [AjaxPro.AjaxMethod] public DataTable GetAjaxDatatable() { var dt = new DataTable( "Table_AX" ); //Method 1 dt.Columns.Add( "column0" , System.Type.GetType( "System.String" )); //Method 2 var dc = new DataColumn( "column1" , System.Type.GetType( "System.Boolean" )); dt.Columns.Add(dc); //Add rows for DataTable //Initialize the row DataRow dr = dt.NewRow(); dr[ "column0" ] = "AX" ; dr[ "column1" ] = true ; dt.Rows.Add(dr); return dt; } //return list<string> [AjaxPro.AjaxMethod] public List<String> GetAjaxListString() { var list = new List<String> { "abc" , "def" }; return list; } //return list<string> [AjaxPro.AjaxMethod] public void SetAjaxObj(BehaviourInfo behaviourInfo) { System.Console.WriteLine(behaviourInfo.Behaviour); } } } |
DataClass.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Runtime.Serialization; namespace AjaxProTest { [DataContract] public class BehaviourInfo { [DataMember(Order = 0)] public string Behaviour { get ; set ; } [DataMember(Order = 1)] public int Lev1 { get ; set ; } [DataMember(Order = 2)] public int Lev2 { get ; set ; } [DataMember(Order = 3)] public bool IsAuto { get ; set ; } } [DataContract] public class User { [DataMember(Order = 0)] public string Name { get ; set ; } [DataMember(Order = 1)] public string Address { get ; set ; } [DataMember(Order = 2)] public string Sex { get ; set ; } } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AjaxProTest._Default" %> <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < head runat="server"> < title >Untitled Page</ title > </ head > < script language="javascript"> //get string function TestString() { AjaxProTest._Default.GetAjaxString("Rocky", TestStringRetrun); } function TestStringRetrun(res) { alert(res.value); } //get object. function TestObject() { AjaxProTest._Default.GetAjaxObject(TestObjectReturn); } function TestObjectReturn(res) { alert(res.value.Behaviour); } //get Datatable. function TestDatatable() { AjaxProTest._Default.GetAjaxDatatable(TestDatatableReturn); } function TestDatatableReturn(res) { var dt = res.value; alert("column0:" + dt.Rows[0].column0 + "\n column1:" + dt.Rows[0].column1); } //get List< String > function TestListString() { AjaxProTest._Default.GetAjaxListString(TestListStringReturn); } function TestListStringReturn(res) { var list = res.value; var str = ""; for(var i=0;i< list.length ;i++){ str = str + list[i] + "\t"; } alert("List:" + str); } //...get其他类推 //set demo function SetObject() { var Obj = new Object(); Obj.Behaviour = "Rocky..."; Obj.Lev1 = 1; Obj.Lev2 = 2; Obj.IsAuto = true; AjaxProTest._Default.SetAjaxObj(Obj); } </script> < body > < form id="form1" runat="server"> < div > Get something demo< br > < input type="button" value="TestString" onclick="TestString();"/> < input type="button" value="TestObject" onclick="TestObject();"/> < input type="button" value="TestDatable" onclick="TestDatatable();"/> < input type="button" value="TestListString" onclick="TestListString();"/> < hr /> Set something demo< br /> < input type="button" value="SetObject" onclick="SetObject();"/> </ div > </ form > </ body > </ html > |
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。