前段事件在網上看到一個基于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