源码下载
1.配置文件设置:
<httpHandlers>
<add verb="*" path="*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
</httpHandlers>
2.编写自定义方法(可选,但用这个方法性能可以提高10倍左右,原来注册要300ms,优化后只要10-15ms)
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
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);
}
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
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.编写服务器端方法
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
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代码==
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
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代码==
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->
<
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>