webservice结合dhtml的简单例子(一,webservice)
2024-08-26 00:15:33
供稿:网友
前段事件在网上看到一个基于web的财务系统,它是通过activex实现的,实际上如果用webservice结合dhtml,那完全可以抛开activex。下面是个简单的例子。
首先是webservice , 很简单,我就不详细说明了,看注释就可以了。
文件 study.asmx.cs
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.diagnostics;
using system.web;
using system.web.services;
using system.xml.serialization ;
namespace studyxml
{
/// <summary>
/// <br>一个webservice的例子</br>
/// <br>author:[email protected]</br>
/// <br>date: 2001/12/21</br>
/// <br>history: 2001//12/21完成</br>
/// </summary>
/// <remarks>
/// 这个webservice实现的功能很简单
/// 主要功能有两个,一个是取得预定义的item数组
/// 另一个是保存record类型的纪录
/// </remarks>
public class study : system.web.services.webservice
{
private arraylist m_arritems ;
private arraylist m_arrreocrds ;
public study()
{
//codegen: this call is required by the asp.net web services designer
initializecomponent();
this.m_arrreocrds = new arraylist() ;
this.m_arritems = new arraylist() ;
//增加一些实验数据
for(int i = 0 ; i < 100 ; i ++)
{
this.m_arritems.add(new item("itemname" + i.tostring()
, "itemvalue" + (i + 1).tostring())) ;
}
}
/// <summary>
///
/// </summary>
/// <param name="a_stritemname">item name</param>
/// <returns>item对象</returns>
private item getitem(string a_stritemname)
{
//throw(new exception(server.urldecode(a_stritemname))) ;
for(int i = 0 ; i < this.m_arritems.count ; i ++)
{
item item = (item)this.m_arritems[i] ;
if(item.name == server.urldecode(a_stritemname).trim())
{
return item ;
}
}
return null ;
}
#region component designer generated code
//required by the web services designer
private icontainer components = null;
/// <summary>
/// required method for designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void initializecomponent()
{
}
/// <summary>
/// clean up any resources being used.
/// </summary>
protected override void dispose( bool disposing )
{
if(disposing && components != null)
{
components.dispose();
}
base.dispose(disposing);
}
#endregion
[webmethod]
public void additem(string a_strname , string a_strvalue)
{
this.m_arritems.add(new item(a_strname , a_strvalue));
}
/// <summary>
/// 取得item列表
/// </summary>
/// <returns>arraylist</returns>
[webmethod]
[xmlinclude(typeof(item))]
public arraylist getitems()
{
return this.m_arritems ;
}
/// <summary>
/// 保存纪录
/// </summary>
/// <param name="a_stritemname"></param>
/// <param name="a_strdemoname"></param>
/// <param name="a_intdemoamount"></param>
/// <returns>如果成功返回false,否则返回false</returns>
[webmethod]
public bool saverecord(string a_stritemname
, string a_strdemoname , int a_intdemoamount)
{
try
{
item item = this.getitem(a_stritemname) ;
if(item != null)
{
this.m_arrreocrds.add(new record(this.m_arrreocrds.count + 1
, item
, new demo(a_strdemoname , a_intdemoamount))) ;
}
else
{
throw(new exception("指定item的name错误!")) ;
}
return true ;
}
catch(exception e)
{
throw(new exception(e.tostring())) ;
//return false ;
}
}//end method
}//end class
/// <summary>
/// 一个简单的类,用来对应象select的option这类东西
/// </summary>
public class item
{
private string m_strname ;
private string m_strvalue ;
public string name
{
get
{
return this.m_strname ;
}
set
{
this.m_strname = value ;
}
}
public string value
{
get
{
return this.m_strvalue ;
}
set
{
this.m_strvalue = value ;
}
}
public item(string a_strname , string a_strvalue)
{
this.m_strname = a_strname ;
this.m_strvalue = a_strvalue ;
}
public item()
{
this.m_strname = "" ;
this.m_strvalue = "" ;
}
}//end class
/// <summary>
/// 简单的示例用类
/// 结构很简单,三个成员变量
/// 一个int类型的编号,
/// 一个item类型,一个demo类型
/// </summary>
public class record
{
private int m_intid ;
private item m_objmyitem ;
private demo m_objmydemo ;
public record()
{
this.m_intid = 0 ;
this.m_objmydemo = new demo() ;
this.m_objmyitem = new item() ;
}
public record(int a_intid , item a_objitem , demo a_objdemo)
{
this.m_intid = a_intid ;
this.m_objmydemo = a_objdemo ;
this.m_objmyitem = a_objitem ;
}
}//end calss
/// <summary>
/// 一个简单的示例用类
/// 结构很简单,只有两个成员变量,一个name , 一个amount
/// </summary>
public class demo
{
private string m_strname ;
private int m_intamount ;
public string name
{
get
{
return this.m_strname ;
}
set
{
this.m_strname = value ;
}
}
public int amount
{
get
{
return this.m_intamount ;
}
set
{
this.m_intamount = value ;
}
}
/// <summary>
/// 构造函数
/// </summary>
public demo()
{
this.m_intamount = 0 ;
this.m_strname = "" ;
}
/// <summary>
/// 重载构造函数
/// </summary>
/// <param name="a_strname"></param>
/// <param name="a_intamount"></param>
public demo(string a_strname , int a_intamount)
{
this.m_intamount = a_intamount ;
this.m_strname = a_strname ;
}
}//end class
}//end namespace