使用vb.net从codebehind调用Javascript函数

时间:2014-03-30 09:34:26

标签: javascript asp.net vb.net

我想调用javascript函数并将参数传递给它,并使用vb.net读取返回的值。

我的Javascript功能是

  function calcHash(MerchID,OrdNo,Amt,RCode){
    //order no. numerical values only

for (var i=0; i<strlen; i++)
{
        var x = OrdNo.substring(i,i+1)
            if (isNaN(x))
        else strDigit = strDigit + String(x);
}
        OrdNo = strDigit

    //declare variables and assign calculated values
    var HashA = OrdNo * Amt;
    var HashB = MerchID * Amt;
    var HashC = MerchID * OrdNo;
    var TotalHash = String((HashA + HashB + HashC) / (parseInt(MerchID) + parseInt(RCode)));

    //assign only 6 decimal places value
    if (TotalHash.indexOf(".") != -1)
        TotalHash = TotalHash.substr(0,TotalHash.indexOf(".")+7);
    else
        TotalHash = TotalHash + ".000000";

    document.form1.HashCount.value = TotalHash;
}

我在vb.net中使用了这行代码(代码背后(:

   ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "calcHashFunction", "calcHash();", True)

但我仍然不知道如何将参数传递给此函数以及如何读取返回的值。

2 个答案:

答案 0 :(得分:0)

你可以这样做:

ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "calcHashFunction", "var val=calcHash(1,2,3,4);", True)

现在您在val变量中返回了值,您可以在客户端使用该变量。

使用此功能:

function calcHash(MerchID, OrdNo, Amt, RCode) {
    //order no. numerical values only

    for (var i = 0; i < strlen; i++) {
        var x = OrdNo.substring(i, i + 1)
        if (isNaN(x))
        else strDigit = strDigit + String(x);
    }
    OrdNo = strDigit

    //declare variables and assign calculated values
    var HashA = OrdNo * Amt;
    var HashB = MerchID * Amt;
    var HashC = MerchID * OrdNo;
    var TotalHash = String((HashA + HashB + HashC) / (parseInt(MerchID) + parseInt(RCode)));

    //assign only 6 decimal places value
    if (TotalHash.indexOf(".") != -1) TotalHash = TotalHash.substr(0, TotalHash.indexOf(".") + 7);
    else TotalHash = TotalHash + ".000000";

    document.form1.HashCount.value = TotalHash;
    return TotalHash;
}

因此,在您的val变量中,您将获得TotalHash

你也可以这样使用它:

ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "calcHashFunction", "var val=calcHash(" + Merchantid + "," + OrderNo + "," + Amount + "," + ReturnCode + ");", True) 

答案 1 :(得分:0)

您可以将返回的值分配给javascript中的隐藏字段,并在服务器端使用它

function calcHash(MerchID, OrdNo, Amt, RCode) 
{
    var TotalHash = String((HashA + HashB + HashC) / (parseInt(MerchID) + parseInt(RCode)));
    var hid=document.getElementById('<hid.ClientID');
    hid.value=TotalHash;
}