======================================================
注:本文源代码点此下载
======================================================
一个ajax的例子
总述
最近公司有个项目,是用b/s结构实现呼叫中心,因为以前有c/s结构的程序,所以只能在后来开发的程序去套接原来的程序。坐席板卡厂家提供了一个ocx,意思用把ocx引入到网页中进通与后台cti通讯,我这里的工作就是跟据它返回的事件来写数据库。看起来似乎没有什么技术上障碍了,但来后的实验中发现了一个大问题。在网页中调用ocx的方法全用的是脚本语言(我用的是javascript),但在asp.net中,提交数据库的搞作是要刷新页面的。这样,当坐席人员签到后,再向数据库服务器端提交数据时,页面刷新,由于脚本在页面刷新后,所有的脚本代码要重新加载一遍,这样,坐席就回到了没签到状态。这样,就有了个冲突,坐席状态不想页面刷新,写数据库要页面刷。
怎么办,幸好,“山西dotnet俱乐部”qq群的“天赐神兵”说用ajax。说是这个可以满足我的需求。于是就看看了,把所得结果共享给大家。
本例是引用了ajax.net的一个dll做的。首先在vs2005的项目中引用一个ajaxpro.2.dll运行库。
1、aspx页面代码
html xmlns="http://www.w3.org/1999/xhtml" >
head runat="server">
title>无标题页title>
meta http-equiv="content-type" content="text/html; charset=gb2312">
script language="javascript" src="log.js">
script>
head>
body>
form name="form1" runat="server" id="form1" >
input onclick="wridata();" class="border" type="button" value="写数据库" name="sub">
form>
body>
html>
在这里,只需调用js里的一个文件就可以了。其中部份是用来引于ocx的,它的方法写入js文件中,这里它的方法就不写了。
2、js页面代码
function add(bz)
{
test.wridata(bz,show);
}
function show(val)
{
alert(val.value);
}
其中test,cs文件中的类名,wridata为cs文件中的函数,bz为参数。show()方法主要是提示。
3、cs文件代码
using system;
using system.data;
using system.data.sqlclient;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
public partial class log : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
ajaxpro.utility.registertypeforajax(typeof(log));
}
[ajaxpro.ajaxmethod]
public string wridata(int bz)
{
string conn = "server=.;database=databasename;uid=sa;pwd=123456";
sqlconnection con = new sqlconnection(conn);
con.open();
sqlcommand com = new sqlcommand();
com.connection = con;
com.commandtext = "update zxdab set zbbz='" + bz + "',listen='" + bz + "'";
com.executenonquery();
return "写数据库成功" ;
}
}
其中ajaxpro.utility.registertypeforajax(typeof(test));是用来注册cs代码中的test类的。每个自定议方法前必需加[ajaxpro.ajaxmethod],wridata()方法是用来写数据库的。
4、webconfig文件的修改
在system.web>下加入
httphandlers>
add verb="post,get" path="ajaxpro/*.ashx" type="ajaxpro.ajaxhandlerfactory, ajaxpro.2"/>
httphandlers>
这里是用来注册httphandlers的。
好了,收工,这样一个不
======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/