使用javascript中的参数调用ASP.net函数

时间:2022-04-09 15:59:03

I have two ASP.Net functions in server side (code behind):

我在服务器端有两个ASP.Net函数(代码隐藏):

string GetError();
string GetErrorAtIndex(int, int);

In client side code, I use javascript to call the first function and it works fine

在客户端代码中,我使用javascript调用第一个函数,它工作正常

function test() {
    var x = <%= GetError() %>;
    alert('x');
}

However, am unable to call the second function which takes two parameters:

但是,我无法调用带有两个参数的第二个函数:

function test2() {
    var param1 = 10;
    var param2 = 100;
    var x = <%= GetErrorAtIndex(param1, param2) %>;
    alert('x');
}

I get the error

我收到了错误

CS0103: The name 'param1' does not exist in the current context

I understand that this is because the javascript local variable won't have visibility in the ASP.Net call. I then thought of using HiddenFields to store/pass parameters, but am unable to do that.

我知道这是因为javascript本地变量在ASP.Net调用中不具备可见性。然后我想到使用HiddenFields来存储/传递参数,但我无法做到这一点。

Any hints/inputs would be appreciated.

任何提示/输入将不胜感激。

Thanks!

2 个解决方案

#1


2  

Look at the __doPostBack() method to send back Event Targets and arguments.

查看__doPostBack()方法以发回事件目标和参数。

#2


3  

Because param1 is a JavaScript variable not an ASP.NET variable.

因为param1是一个JavaScript变量而不是ASP.NET变量。

#1


2  

Look at the __doPostBack() method to send back Event Targets and arguments.

查看__doPostBack()方法以发回事件目标和参数。

#2


3  

Because param1 is a JavaScript variable not an ASP.NET variable.

因为param1是一个JavaScript变量而不是ASP.NET变量。