ajax使用步骤
1。在项目下的Bin文件夹中加入AjaxPro.2.dll文件。
2。在Web.Config文件中,声明AjaxPro的配置信息。


1
<!-- AjaxPro 的配置信息 -->
2
<configSections>
3
<sectionGroup name="ajaxNet">
4
<section name="ajaxSettings" type="AjaxPro.AjaxSettingsSectionHandler, AjaxPro.2"/>
5
</sectionGroup>
6
</configSections>
7
<ajaxNet>
8
<ajaxSettings>
9
<urlNamespaceMappings>
10
<!-- <add path="sample" type="AJAXDemo.WebForm1,AJAXDemo" /> -->
11
</urlNamespaceMappings>
12
<jsonConverters>
13
<add type="AJAXDemo.Examples.Classes.PersonConverter,AJAXDemo"/>
14
</jsonConverters>
15
</ajaxSettings>
16
</ajaxNet>
17
<!-- /AjaxPro 的配置信息 -->
3。在要使用Ajax文件的.cs文件中,先声明名字空间。如:using AjaxPro;
4。用到类,先注册。在要使用Ajax文件的.cs文件中注册。如:
1
GlobalCommand.myvalidate("yewu/default.aspx");
2
Utility.RegisterTypeForAjax(typeof(yewu_Default));
5。注册完类,就可以在前端页面中引用该类。如:
前端Ajax程序:


1
<script type="text/javascript" defer="defer">
2
yewu_Default.onLoading = function(b)
{
3
var l = document.getElementById("loadinfo");
4
l.style.visibility = b ? "visible" : "hidden";
5
}
6
function doTest2(ZJKSID)
7
{
8
if(document.all.JYLXIDHIDDEN.value=="")
9
{
10
alert('请选择检验类型,或设置检验类型的代码!');
11
}
12
else
13
{
14
yewu_Default.getData(ZJKSID,escape(document.all.JYLXIDHIDDEN.value), doTest2_callback);
15
}
16
}
17
function doTest2_callback(res)
18
{
19
mystr =unescape(res.value);
20
mystr=mystr.replace("XXX",document.all.JYLXIDHIDDEN.value);
21
document.all.BGBH.value=mystr;
22
document.all.BGBH1.value=mystr;
23
document.all.ZJKS_SPBH.value=mystr;
24
}
25
</script>
后台Ajax函数:


1
[AjaxMethod]
2
public string getData(string inputString,string JYLXIDString)
3
{
4
5
string myJYLXIDString;
6
7
myJYLXIDString = Server.UrlDecode(JYLXIDString);
8
//System.Threading.Thread.Sleep(1000); //进程睡眠(延时)1秒
9
string temp;
10
temp = "";
11
string inputString1="";
12
if ((inputString == "-1") || (inputString == null) || (inputString == ""))
13
{
14
temp = "";
15
}
16
else
17
{
18
SqlConnection myconn;
19
myconn = new SqlConnection(ConfigurationManager.ConnectionStrings["BaseConnectionString"].ConnectionString);
20
myconn.Open();
21
SqlCommand myCMD1 = new SqlCommand("select bianhao from Shebei_keshi where KeshiID=" + inputString, myconn);
22
SqlDataReader myReader1 = myCMD1.ExecuteReader();
23
if (myReader1.Read())
24
{
25
if (myReader1.IsDBNull(0))
26
{
27
inputString1 = "无";
28
}
29
else
30
{
31
inputString1 = myReader1[0].ToString();
32
}
33
}
34
myReader1.Close();
35
myconn.Close();
36
37
//temp = "select top 1 BGBH from YEWU where left(BGBH,len(BGBH)-4)='(" + inputString1 + JYLXIDString + ")" + DateTime.Today.Year.ToString() + "' and bgbh not like '%X%' order by YEWUID desc";
38
myconn.Open();
39
SqlCommand myCMD = new SqlCommand("select top 1 BGBH from YEWU where left(BGBH,len(BGBH)-4)='" + DateTime.Today.Year.ToString() +inputString1+ "' and bgbh not like '%X%' order by YEWUID desc", myconn);
40
SqlDataReader myReader = myCMD.ExecuteReader();
41
try
42
{
43
if (myReader.Read())
44
{
45
string mystr1 = myReader.GetString(0).ToString();
46
47
mystr1 = mystr1.Substring(6);
48
49
Int32 count1 = Convert.ToInt32(mystr1);
50
51
count1 = count1 + 1;
52
temp = count1.ToString();
53
if (count1 < 10)
54
{
55
temp = "000" + count1.ToString();
56
}
57
else if ((count1 >= 10) && (count1 < 100))
58
{
59
temp = "00" + count1.ToString();
60
}
61
else if ((count1 >= 100) && (count1 < 1000))
62
{
63
temp = "0" + count1.ToString();
64
}
65
else if ((count1 >= 1000) && (count1 < 10000))
66
{
67
temp = count1.ToString();
68
}
69
else
70
{
71
temp = "数据超出最大值";
72
}
73
temp = DateTime.Today.Year.ToString() + inputString1 + temp;
74
}
75
else
76
{
77
temp = DateTime.Today.Year.ToString() + inputString1 + "0001";
78
}
79
}
80
finally
81
{
82
myReader.Close();
83
myconn.Close();
84
}
85
}
86
return Microsoft.JScript.GlobalObject.escape(temp);
87
}
--------------------------------------------------------------------------------------------------------------
Ajax初学总结几点:
A。前端页面中调用.cs中Ajax函数,形参个数自定义。但如果要返回值,那么形参后面必须要带过去一个接收返回值得前端处理函数名。
B。后台页面中必须有,前端页面中调用的函数。
C。后台Ajax函数前要加Ajax标记,如:[AjaxMethod]
D。后台Ajax函数可以不返回值,也可以返回值,但如果有返回值,无论什么情况,只能返回一个值。如果需要返回多个,用数组存储后返回。
E。前端如果有接收返回值并进行处理、操作的函数,参数也必须是一个。