前两天在网上下了个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,然后输出。
[ajax.ajaxmethod]
public int addajaxtable(string name)
{
//输入一个字符串,然后更新
sqlconnection conn = new sqlconnection( system.configuration.configurationsettings.appsettings["connectionstring"] );
sqlcommand cmd = new sqlcommand("insert into ajaxtable(name) values(’"+name+"’)", conn);
cmd.connection.open();
int result = cmd.executenonquery();
conn.dispose();
cmd.dispose();
return result;
}
[ajax.ajaxmethod]
public string getajaxtable()
{
//这个方法就是拿到datagrid生成出来的html
sqlconnection conn = new sqlconnection(system.configuration.configurationsettings.appsettings["connectionstring"]);\
sqlcommand cmd = new sqlcommand("select * from ajaxtable order by id", conn);
sqldataadapter ap = new sqldataadapter( cmd );
dataset ds = new dataset();
ap.selectcommand.connection.open();
ap.fill( ds, "db" );
conn.dispose();
cmd.dispose();
//实例化一个datagird类并设置好数据源
datagrid dg = new datagrid();
dg.datasource = ds.tables["db"];
dg.databind();
//实例化一个htmltextwriter的类
system.text.stringbuilder strb = new system.text.stringbuilder();
system.io.stringwriter sw = new system.io.stringwriter( strb );
system.web.ui.htmltextwriter htw = new htmltextwriter( sw );
//执行控件的render并输出到htmltextwriter里
dg.rendercontrol( htw );
string s = strb.tostring();
return s;//最后就是返回这个html啦
}
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 οnlοad="loadgrid()">
<form id="form1" method="post" runat="server">
<input type="text" id="addtextbox" maxlength="10"><input type="button" value="添加" οnclick="javascript:addajax(form.addtextbox.value);">
<table>
<tr>
<td id="uctd"></td>
</tr>
</table>
</form>
</body>
</html>