CS中使用Ajax的步驟

来源:互联网 发布:淘宝客ab单处罚 编辑:程序博客网 时间:2024/05/09 21:29

CS中使用Ajax的步驟

1.引用Ajax

首先,當然是將Ajax.dll加入參考;

然后,在Page類引入Ajaxusing Ajax;

2.#region "Form Load"區塊中SetCustomerAttributes方法中將類(Page類,例如頁面WPS011)加到Ajax中:

Ajax.Utility.RegisterTypeForAjax(typeof(WPS011));  //將類WPS011加到Ajax

3.准備工作做好了,現在可以在后台用Ajax寫函數了,例如:

1:計算成交金額

/*************************************************************

'Function名稱:CalculateDealAmt

'*功能說明:計算成交金額  公式:成交金額 = 成交股數 * 單價

'*參數說明:

'*返回值 :

'*************************************************************/

[AjaxMethod()]

public double CalculateDealAmt(string strDealQuantity,string strUnitPrice)

{

double retValue = 0.0;

     try

     {

retValue = System.Convert.ToDouble(strDealQuantity) *                       System.Convert.ToDouble(strUnitPrice);

     }

     catch(Exception exc)

     {

          

     }

     return retValue;

}

2:返回string型的例子

/* 修改提示:Check Function Region部分,程序編寫者可根據程式實際操作需要更改

* Region說明:

* 檢查需要輸入欄位的錄入信息是否符合要求

* */

[AjaxMethod()]

public string FunCheck(string strBusinType,

                     string strCustID,

                     string strCustSrNo,

                     string strTxModuleID,

                     string strTxDate,

                     string strCloseFlag,

                     string strCloseFlagE)

{              

     string retValue = "0";

     bool bolReturn=true;

     DataSet dstErrorList=new DataSet();

     comGCtlFun=new GCtlFun();

     comAccessCTL=new AccessCTL(this);

     comGFunction=new GFunction();

    

     comInterfaces=Activator.CreateInstance(typInterfaces);

     DataRow drwTemp;

     dstErrorList = comGCtlFun.MakeDataSet("errfield,errdescription", "1,1", "100,300","");    

     Type typAccntDate = Type.GetTypeFromProgID("HI_SysTR.HI_AccntDate");

                           object comAccntDate = Activator.CreateInstance(typAccntDate);

     bool tempBool = Convert.ToBoolean(typAccntDate.InvokeMember("CheckTxModuleDate",

                     BindingFlags.Default | BindingFlags.InvokeMethod,

                     null,comAccntDate,new object[]{strBusinType.Trim(),

                                                     strCustID.Trim(),

                                                     strCustSrNo.Trim(),

                                                     strTxModuleID.Trim(),

                                                     strTxDate.Trim(),

                                                     strCloseFlag.Trim(),

                                                     strCloseFlagE.Trim()}));

    

     if(!tempBool)

     {

           drwTemp = dstErrorList.Tables[0].NewRow();

           drwTemp["errfield"] = "交易日";

           drwTemp["errDescription"] = "該交易日不可承作任何交易!請檢查!";

           dstErrorList.Tables[0].Rows.Add(drwTemp);

           bolReturn = false;

     }

    

     //顯示錯誤欄位清單

if (bolReturn == false)

     {

           //TODO:GCtlFun.gShowErrorMsg

           string strErrMsg = "";

           for(int intRow=0; intRow < dstErrorList.Tables[0].Rows.Count; intRow++ )

           {

                strErrMsg += dstErrorList.Tables[0].Rows[intRow]["errfield"].ToString() + ":"

+ dstErrorList.Tables[0].Rows[intRow]["errdescription"].ToString() + "/n";

           }

           //comGCtlFun.gShowNotifyMsg(this.Page,"新增失敗!//n"+strErrMsg ,"錯誤提示","454","367" ); 

           retValue =  "1|新增失敗!/n"+strErrMsg;

     }

               

     return retValue;

}

注意:

A[AjaxMethod()]不可以忘記寫,否則前台會調不到該函數。

B.函數的參數一般應該是string型的。

C.函數的返回類型則視需要而定,如例1返回double型,例2則返回string型。

4.現在就可以在前台調用寫好的函數了。例如現在要在前台調用例1寫好的函數,則:

/*************************************************************

'*Function名稱:CalculateDealAmt

'*功能說明:計算成交金額 公式:成交金額 = 成交股數 * 單價

'*參數說明:

'*返回值 :

'*************************************************************/

function CalculateDealAmt()

{

strDealAmt.value = WPS011.CalculateDealAmt(strDealQuantity.value.replace(//,/g,''),strUnitPrice.value.replace(//,/g,'')).value;

}

 注意:

A.因為在步驟2中已經將類WPS011加到Ajax中,所以前台就可以直接寫WPS011.CalculateDealAmt來調用函數CalculateDealAmt了。

5.好了,現在到調試的工作了。。。。呵呵。。。。。。。。。

 
原创粉丝点击