以往EEP會建議使用者在對資料進行修改、過帳等情況下,使用Server端的方法來實現。那麽這一小節的內容為大家介紹如何使用ServerMethod從server端自定義取出資料,回傳回Client。下面以rCustomers為例。
Step1>       
首先我們找到dgView這個DataGrid元件,ToolItems屬性編輯器進入,我們添加一個按鈕,用來呼叫ServerMethod程式。

Step2>       
將Name和Text屬性設定為CallServerMethod,onClick輸入serverMethod。表示呼叫js的方法serverMethod。

Step3>       
js的部分寫入如下的方法:

function serverMethod() {
var index = $('#dgView').datagrid('getSelectedIndex');//取得當前選中的資料
var row = $('#dgView').datagrid('getRows')[index];
$.ajax({
type: "POST",
url: '../handler/JQDataHandle.ashx?RemoteName=sCustomers.Customers', //連接的Server端,command
data: "mode=method&method=" + "CallMethod" + "¶meters=" + row.CustomerID, //method後的參數為server的Method名稱 parameters後為端的到後端的參數這裡傳入選中資料的CustomerID欄位
cache: false,
async: false,
success: function (data) {
alert(data);//回傳值
}
});
}
Step4>       
上面步驟完成後,就來到server端,首先要在sCustomers中的Component.cs中添加一個名為CallMethod的方法。內容如下:
程式參數說明:傳入一個Object數組,數組長度為1,參數為js中的Parameters的值。
回傳一個object數組,第一個參數固定格式為0,其餘參數可根據用戶自己的需要進行設定。
public object[] CallMethod(object[] objParam)
        {
           
string CustomerID = (string)objParam[0];
           
string OrderID = "";
           
//創建資料庫連接
           
IDbConnection connection =
(IDbConnection)AllocateConnection(GetClientInfo(ClientInfoType.LoginDB).ToString());
           
//打開連接
           
if (connection.State != ConnectionState.Open)
            {
               
connection.Open();
            }
           
//開始transaction
           
IDbTransaction transaction = connection.BeginTransaction();
           
try
            {
               
//取得該間客戶擁有哪些訂單
               
string selectSql = "select OrderID from Orders where CustomerID =
'" + CustomerID + "'";
               
DataSet ds = this.ExecuteSql(selectSql, connection, transaction);
               
if (ds.Tables[0].Rows.Count != 0)
               
{
                   
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                   
{
                        OrderID +=
ds.Tables[0].Rows[i]["OrderID"].ToString() + ",";
                   
}
                   
OrderID = OrderID.Substring(0, OrderID.Length - 1); //去除最後一個標點符號
               
}
               
return new object[] { 0, "訂單號碼:" + OrderID
};//回傳訂單號碼
            }
           
catch
            {
               
transaction.Rollback();
               
return new object[] { 0, false };
            }
           
finally
            {
               
ReleaseConnection(GetClientInfo(ClientInfoType.LoginDB).ToString(),
connection);
           
}            
        }
Step5>       
上面這些程式完成後,請回到該cs的頂部,添加using。
using System.Data;
Step6>       
請在設計畫面的serviceManager元件上添加新增的服務。


DelegateName為程式實際上的名稱,ServiceName為程式被呼叫的名稱,在此設置一樣即可。
Step7>       
修改完server端後,請重新建置sCustomers。
Step8>       
打開客戶管理表單,我們來執行一下效果:
首先選中一筆客戶資料,再點選Grid上面的「」按鈕。

此時會回傳該公司在訂單表中所擁有的訂單號碼。

Related Topics