个人常用的东西收集起来,也给大家一起分享哈。
AjaxPro.2.dll在VS2005使用中的基本使用
,下面介绍了基本的配置和最简单的使用方法
AJAX不用说大家都明白,不明白的请百度或Google一下人家比我说的专业,笔者写此篇文章不在于介绍多深奥的东西,这样只举一个很简单的例子来引导到大家.此例只为让大家明白AJAX的基本的调用需要做那些事情笔者用的是VS2005引用AJAX2.0程序集(
AjaxPro.2.dll) 。
在web.config文件中的<system.web>加入以下设置
<
httpHandlers
>
<
add
verb
="POST,GET"
path
="ajaxpro/*.ashx"
type
="AjaxPro.AjaxHandlerFactory,AjaxPro.2"
/>
</
httpHandlers
>
3. 创建一个页面Default.aspx
4. 在Default.aspx.cs文件的Page_Load中注册AJAX可调用的类的名称
protected
void
Page_Load(
object
sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));
}
5. 创建AJAX可调用的方法:
[AjaxPro.AjaxMethod]
public
string
SetTb(
string
name)
{
return name;
}
注意:[
AjaxPro.AjaxMethod]是定义
AjaxPro可调用的方法,是必须要注明的
6. 前台代码:
<
%@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Default.aspx.cs
"
Inherits
=
"
_Default
"
%
>
<
!DOCTYPE html
PUBLIC
"
-//W3C//DTD XHTML 1.0 Transitional//EN
"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
<
head runat
=
"
server
"
>
<
title
>
无标题页
</
title
>
<
script language
=
"
javascript
"
type
=
"
text/javascript
"
>
//
<
!CDATA[
Function comit_onclick()
function comit_onclick()
{
var name=document.getElementById("tb1").value;
_Default.SetTb(name,callback);
}
Function callback()function callback(res)
{
document.getElementById("tb").value=res.value;
}
// ]]>
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="tb1" runat="server"></asp:TextBox><br />
<input id="comit" type="button" value="Ok" onclick="return comit_onclick()" />
<br /><asp:TextBox ID="tb" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
注意:这里值得注意的地方是 _Default.SetTb(name,callback);这句话是为了调用_Default.aspx.cs后台代码中SetTb这个方法的,如果这个方法没有要传递的参数则指明返回的处理方法是哪一个就OK了,写成_Default.SetTb(callback);
源码下载
1.配置文件设置:
<httpHandlers>
<add verb="*" path="*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
</httpHandlers>
2.编写自定义方法(可选,但用这个方法性能可以提高10倍左右,原来注册要300ms,优化后只要10-15ms)
public
static
void
RegAjax(HtmlHead Header,Type type)
{
//
用来代替 AjaxPro.Utility.RegisterTypeForAjax(Type type);方法
string
assemblyName
=
type.FullName
+
"
,
"
+
type.Assembly.FullName.Substring(
0
, type.Assembly.FullName.IndexOf(
"
,
"
));
if
(type.Assembly.FullName.StartsWith(
"
App_Code.
"
))
assemblyName
=
type.FullName
+
"
,App_Code
"
;
Literal l
=
new
Literal();
l.Text
=
"
\n<script type=\
"
text
/
javascript\
"
src=\
"
/
ajaxpro
/
prototype.ashx\
"
></script>\n
"
+
"
<script type=\
"
text
/
javascript\
"
src=\
"
/
ajaxpro
/
core.ashx\
"
></script>\n
"
+
"
<script type=\
"
text
/
javascript\
"
src=\
"
/
ajaxpro
/
converter.ashx\
"
></script>\n
"
+
"
<script type=\
"
text
/
javascript\
"
src=\
"
/
ajaxpro
/
"
+ assemblyName +
"
.ashx\
"
></script>\n
"
;
Header.Controls.Add(l);
}
public
static
void
RegAjax(HtmlHead Header)
{
//
用来代替 AjaxPro.Utility.RegisterTypeForAjax(Type type);方法
Literal l
=
new
Literal();
l.Text
=
"
\n<script type=\
"
text
/
javascript\
"
src=\
"
/
ajaxpro
/
prototype.ashx\
"
></script>\n
"
+
"
<script type=\
"
text
/
javascript\
"
src=\
"
/
ajaxpro
/
core.ashx\
"
></script>\n
"
+
"
<script type=\
"
text
/
javascript\
"
src=\
"
/
ajaxpro
/
converter.ashx\
"
></script>\n
"
+
"
<script type=\
"
text
/
javascript\
"
src=\
"
/
ajaxpro
/
fisha.DAL.PersionDao,fisha.ashx\
"
></script>\n
"
;
Header.Controls.Add(l);
}
//
fisha.DAL.PersionDao 用来存方[AjaxPro.AjaxMethod]的类名,命名空间+类名
//
fisha.ashx 中的fisha为程序集的名称
3.编写服务器端方法
namespace
fisha.DAL
{
public
class
PersionDao
{
public
DataTable GetTable()
{
DataTable dt
=
new
DataTable(
"
persion
"
);
DataColumn dc1
=
new
DataColumn(
"
name
"
, Type.GetType(
"
System.String
"
));
DataColumn dc2
=
new
DataColumn(
"
age
"
, Type.GetType(
"
System.Int16
"
));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
//
以上代码完成了DataTable的构架,但是里面是没有任何数据的
for
(
int
i
=
0
; i
<
10
; i
++
)
{
DataRow dr
=
dt.NewRow();
dr[
"
name
"
]
=
"
小小
"
;
dr[
"
age
"
]
=
10
;
dt.Rows.Add(dr);
}
return
dt;
}
[AjaxPro.AjaxMethod]
public
IList
<
MyClass
>
GetList()
{
DataTable tab_data
=
GetTable();
IList
<
MyClass
>
myClassList
=
new
List
<
MyClass
>
();
if
(tab_data.Rows.Count
>
0
)
{
for
(
int
i
=
0
; i
<
tab_data.Rows.Count; i
++
)
{
DataRow dr
=
tab_data.Rows[i];
MyClass item
=
new
MyClass { Name
=
dr[
"
name
"
].ToString(), Age
=
Convert.ToInt16(dr[
"
age
"
].ToString())};
myClassList.Add(item);
}
}
return
myClassList;
}
}
public
class
MyClass
{
private
string
name
=
string
.Empty;
private
int
age
=
0
;
public
string
Name
{
get
{
return
name; }
set
{ name
=
value; }
}
public
int
Age
{
get
{
return
age; }
set
{ age
=
value; }
}
}
}
4.新建一个aspx页面 test.aspx
===test.aspx.cs代码==
public
partial
class
test : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
BasicDao.RegAjax(Header,
typeof
(fisha.DAL.PersionDao));
//
用自定义的方法,向客户端注册脚本
}
}
===test.aspx代码==
<
script language
=
"
javascript
"
type
=
"
text/javascript
"
defer
=
"
defer
"
>
function
ShowList()
{
var
res
=
fisha.DAL.PersionDao.GetList().value;
var
tab
=
document.getElementById(
"
tab
"
);
var
tblHtml
=
""
;
if
(res)
{
//
res是服务器返回的一个List<MyClass>集合
for
(
var
i
=
0
;i
<
res.length;i
++
)
{
tblHtml
+=
res[i].Name
+
"
.
"
+
res[i].Age
+
"
<br/>
"
;
//
从上面可以看出可以直接调用List<City>集合中的元素和它们的属性
}
}
tab.innerHTML
=
tblHtml;
}
<
/
script>
<
a href
=
"
javascript:ShowList();void(0);
"
>
here
<
/
a>
<
div id
=
"
tab
"
>
<
/
div>