使用 vb.net 从代码隐藏调用 Javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22742406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call Javascript function from codebehind using vb.net
提问by Dan
I want to call javascript function and pass parameters to it and read the returned value using vb.net .
我想调用 javascript 函数并将参数传递给它并使用 vb.net 读取返回值。
My Javascript function is
我的 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;
}
I used this line of code in vb.net (code behind( :
我在 vb.net 中使用了这行代码(后面的代码(:
ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "calcHashFunction", "calcHash();", True)
But still i dont know how to pass parameters to this function and how to read the returned value.
但是我仍然不知道如何将参数传递给这个函数以及如何读取返回值。
采纳答案by King of kings
you can assign the returned value to a hidden field in javascript and use it on the server side
您可以将返回值分配给 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;
}
回答by Amit Joki
You could do this:
你可以这样做:
ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "calcHashFunction", "var val=calcHash(1,2,3,4);", True)
Now you have the returned value in the valvariable, which you can probably use at the client side.
现在您在val变量中有返回值,您可能可以在客户端使用它。
Use this function:
使用这个功能:
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;
}
So, in your valvariable, you'll have your TotalHash.
因此,在您的val变量中,您将拥有TotalHash.
You can also use it like this:
你也可以这样使用它:
ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "calcHashFunction", "var val=calcHash(" + Merchantid + "," + OrderNo + "," + Amount + "," + ReturnCode + ");", True)

