简体   繁体   中英

Passing a value to code behind function from var

I am trying to pass the pageIndex variable to my code behind from javascript. The code works if I pass any hardcoded value. However, I can't get it to work using var values.

 pageIndex++;
 var sPageIndex = pageIndex.toString;
 document.getElementById('<%= Label1.ClientID %>').innerHTML = '<%= xDoSomething(sPageIndex)%>';

This works:

document.getElementById('<%= Label1.ClientID %>').innerHTML = '<%= xDoSomething(123456)%>';

This is my simple code behind function:

Public Shared Function xDoSomething(sPageIndex As String) As String
        Dim ss As String = sPageIndex
        Try
            If ss.Length < 1 Then
                ss = "smaller than one"
            Else
                ss = ss
            End If
            Return "from code behind [" & ss & "]"

        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function

It looks like the value I'm trying to pass does not have permissions to be accessed! Any ideas how to fix this?

You need to understand the difference between server-side and client-side. At the moment you're mixing the two in a way that simply can't work.

pageIndex++;
var sPageIndex = pageIndex.toString;
document.getElementById('<%= Label1.ClientID %>').innerHTML = '<%= xDoSomething(sPageIndex)%>';

What you're trying to do in the above code is use a variable which is only present on the client-side (ie the browser) as part of your code on the server-side (ie on your IIS server).

On the server sPageIndex doesn't exist - it simply can't use it, and changing it on the browser isn't going to make the slightest bit of difference.

The reason the following bit of code works is that you're giving the function a value of 123456 on the server itself. The server is able to use the value of 123456 and produce the string that you desire.

document.getElementById('<%= Label1.ClientID %>').innerHTML = '<%= xDoSomething(123456)%>';

If you need the .innerHTML to be based on the client-side sPageIndex variable, you either need to send the value of that variable to the server (through query string or a form) so that it can process it as part of the page render... or you can look at something like AJAX which will allow you to go to the server in the background, get the HTML and display it as required.

What you are trying to pass becomes a string. Try this :

pageIndex++;
var sPageIndex = pageIndex;
document.getElementById('<%= Label1.ClientID %>').innerHTML = '<%= xDoSomething('+sPageIndex+')%>';

you have to do the string concatenation.

 pageIndex++;
 var sPageIndex = pageIndex.toString;
 document.getElementById('<%= Label1.ClientID %>').innerHTML = '<%= xDoSomething('+sPageIndex+')%>';

You could set up you server side function as a page method instead. The answer below might help:

https://stackoverflow.com/a/27081733/4773711

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM