sppc42
sppc42

Reputation: 3292

Calling ASP.net function with parameters from javascript

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

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

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

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.

Any hints/inputs would be appreciated.

Thanks!

Upvotes: 0

Views: 1719

Answers (2)

Gjohn
Gjohn

Reputation: 1281

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

Upvotes: 2

Alborz
Alborz

Reputation: 6903

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

Upvotes: 3

Related Questions