原文地址:http://jasmineou.cnblogs.com/archive/2005/09/26/244312.html
1.照着试了半天一直没弄好!!!!
原因:ps1:下载的是AjaxPro
(
1
) 在web.conf文件中
ajaxpro:
<
httpHandlers
>
<
add verb
=
"
POST,GET
"
path
=
"
ajaxpro/*.ashx
"
type
=
"
AjaxPro.AjaxHandlerFactory, AjaxPro
"
/>
</
httpHandlers
>
ajax:
<
httpHandlers
>
<
add verb
=
"
POST,GET
"
path
=
"
ajax/*.ashx
"
type
=
"
Ajax.PageHandlerFactory, Ajax
"
/>
</
httpHandlers
>
(
2
) 在具体的aspx文件中,在该例中test是我所建的项目名称
ajaxpro:
<
script type
=
"
text/javascript
"
src
=
"
/test/ajaxpro/prototype.ashx
"
></
script
>
<
script type
=
"
text/javascript
"
src
=
"
/test/ajaxpro/core.ashx
"
></
script
>
<
script type
=
"
text/javascript
"
src
=
"
/test/ajaxpro/converter.ashx
"
></
script
>
<
script type
=
"
text/javascript
"
src
=
"
/test/ajaxpro/test.ajaxprotest,test.ashx
"
></
script
>
ajax:
<
script src
=
"
/test/cshop/ajax/common.ashx
"
type
=
"
text/javascript
"
></
script
>
<
script src
=
"
/test/cshop/ajax/cshop.ajaxdllcom,cshop.ashx
"
type
=
"
text/javascript
"
></
script
>
(
3
) 在具体的类文件中
ajaxpro:
在函数前以
"
[AjaxPro.AjaxMethod()]
"
开头
ajax:
在函数前以
"
[Ajax.AjaxMethod()]
"
开头.
注:由于是用C#来写的.所以是以中括号来表示.如果ajax能在vb.net下运行的话.估计应该是如下方式:
<
Ajax.AjaxMethod()
>
(
4
)ajax:
修改Global.asax的Application_Start事件,设置Ajax的HandlerPath :
protected
void
Application_Start(Object sender, EventArgs e)
{
Ajax.Utility.HandlerPath
=
"
ajax
"
;
}
注意的是:该版本的.net Ajax需要手工在中Global.asax加上Ajax.Utility.HandlerPath
=
"
ajax
"
;
虽然照着做了,还是不可以
ps2:起初把类DemoMethod建在与网站同一个项目中,提示找不到类,后把类防在类库MyAjaxProLib中
protected
void
Page_Load(
object
sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(
typeof
(MyAjaxProLib.DemoMethod));
}
ps3:在js中找不到类
<
script language
=
"
javascript
"
type
=
"
text/javascript
"
>
function AddAjax(name)
{
MyAjaxProLib.DemoMethods.AddAjaxTable(name);
}
</
script
>
提示找不到对象MyAjaxProLib.DemoMethods郁闷!!!>
原文内容:
ajax.net中与server控件的交互使用(一)
前两天在网上下了个ajax组件体验了一下,感觉很不错。但后来开始想怎样能让它跟server控件交互呢,例如我上输出一个列表,就只有用js一条一条html的输出吗?不!!现在我说说怎样与 datagrid交互。
注:ajax.net的组件可以到此网下载,我用的是for .net 1.1版本的。http://ajax.schwarz-interactive.de/
1. 在引用中添加引用Ajax.dll。(这个很废话)
2.在web.config中建立HttpHandler(这个当然是在system.web串里的)
<
httpHandlers
>
<
add
verb
="POST,GET"
path
="ajax/*.ashx"
type
="Ajax.PageHandlerFactory, Ajax"
/>
</
httpHandlers
>
3.在Global的Application_Start里加上个设置
protected
void
Application_Start(Object sender, EventArgs e)
{
Ajax.Utility.HandlerPath = "ajax";
}
4.新建一个类DemoMethods,这个类里面提供了更新数据库和输出列表的方法。其实主要思想就是获得控件运行后生成的html,然后输出。
1
[Ajax.AjaxMethod]
2
public
int
AddAjaxTable(
string
name)
3
{
4 //输入一个字符串,然后更新
5 SqlConnection conn = new SqlConnection( System.Configuration.ConfigurationSettings.AppSettings["connectionString"] );
6 SqlCommand cmd = new SqlCommand("insert into ajaxTable(name) values('"+name+"')", conn);
7 cmd.Connection.Open();
8 int result = cmd.ExecuteNonQuery();
9 conn.Dispose();
10 cmd.Dispose();
11 return result;
12 }
13
14
[Ajax.AjaxMethod]
15
public
string
GetAjaxTable()
16
{
17 //这个方法就是拿到datagrid生成出来的html
18 SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["connectionString"]);
19 SqlCommand cmd = new SqlCommand("select * from ajaxTable order by id", conn);
20 SqlDataAdapter ap = new SqlDataAdapter( cmd );
21 DataSet ds = new DataSet();
22 ap.SelectCommand.Connection.Open();
23 ap.Fill( ds, "db" );
24
25 conn.Dispose();
26 cmd.Dispose();
27
28 //实例化一个datagird类并设置好数据源
29 DataGrid dg = new DataGrid();
30 dg.DataSource = ds.Tables["db"];
31 dg.DataBind();
32
33 //实例化一个HtmlTextWriter的类
34 System.Text.StringBuilder strb = new System.Text.StringBuilder();
35 System.IO.StringWriter sw = new System.IO.StringWriter( strb );
36 System.Web.UI.HtmlTextWriter htw = new HtmlTextWriter( sw );
37
38 //执行控件的render并输出到HtmlTextWriter里
39 dg.RenderControl( htw );
40
41 string s = strb.ToString();
42
43 return s;//最后就是返回这个html啦
44 }
5.然后再建一个default.js文件,用作存放 js方法
function
AddAjax(name)
{
DemoMethods.AddAjaxTable(name);
LoadGrid();
}
function
LoadGrid()
{
var cc=document.getElementById("UCtd");
cc.innerHTML=DemoMethods.GetAjaxTable().value;
}
6.建一个default.aspx,在pageload事件里面加个注册的东西
private
void
Page_Load(
object
sender, System.EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(AjaxTestPrjLib.DemoMethods));
}
7.最后就是default.aspx的html和js了,呵呵
<%
@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="AjaxTextPrjWeb._default"
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>
<
HTML
>
<
HEAD
>
<
title
>
default
</
title
>
<
meta
name
="GENERATOR"
Content
="Microsoft Visual Studio .NET 7.1"
>
<
meta
name
="CODE_LANGUAGE"
Content
="C#"
>
<
meta
name
="vs_defaultClientScript"
content
="JavaScript"
>
<
meta
name
="vs_targetSchema"
content
="http://schemas.microsoft.com/intellisense/ie5"
>
<
script
language
="javascript"
src
="default.js"
></
script
>
</
HEAD
>
<
body
onload
="LoadGrid()"
>
<
form
id
="Form1"
method
="post"
runat
="server"
>
<
INPUT
type
="text"
id
="AddTextBox"
maxlength
="10"
><
INPUT
type
="button"
value
="添加"
onclick
="javascript:AddAjax(form.AddTextBox.value);"
>
<
table
>
<
tr
>
<
td
id
="UCtd"
></
td
>
</
tr
>
</
table
>
</
form
>
</
body
>
</
HTML
>
这样,就能使用datagrid来输出表格了,呵呵。总体思路其实还是比较简单的,日后有兄弟做出更好的,请贴出来,而我也会继续努力的。谢谢....继续工作去....
------------------------------------------
2010-1-22
有幸用到::
1.引入AjaxPro.dll
2.<!-- 声明使用AjaxPro-->
<httpHandlers>
<add path="*.ashx" verb="*" type="AjaxPro.AjaxHandlerFactory"/>
</httpHandlers>
3.(1)在页面加载时注册类(其中default为类名)
AjaxPro.Utility.RegisterTypeForAjax(typeof(Default));
(2)在需要调用的方法添加头部||||||||||||上面那次失败,可能就这里出问题了
[AjaxPro.AjaxMethod(HttpSessionStateRequirement.Read)]
public int login(string name){}
(3)在前台js中直接使用声明过的方法
var str =Default.login("a").value;