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