0

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?

1
  • You need to understand the difference between server-side and client-side. You are currently trying to mix both, and that (as you have found) won't work in this situation Commented Apr 10, 2015 at 13:37

4 Answers 4

1

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 (i.e. the browser) as part of your code on the server-side (i.e. 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.

Sign up to request clarification or add additional context in comments.

7 Comments

I'm afraid your answer is very correct. I can't use AJAX because this is a dotnetnuke module and it's very complicated to call web methods from a module. Do you have any other suggestions to make this work?
Sorry @always, dotnetnuke is 100% outside my knowledge range (never touched it, know nothing about it). Are you saying that it's impossible to make an AJAX call from the client-side? Does the technology not allow you to make a different server-side page with the sole responsibility to take the sPageIndex, run the server-side function and return the HTML?
I could pass query strings etc but what I'm trying to do is to avoid a postback.
@always, if AJAX isn't available to you, then post-back is your only option. xDoSomething exists on the server, and the server alone. If you need to call it, you have to send some sort of request to the server, either through post-back, AJAX, SignalR, etc.
@always - as I say, I know nothing about dotnetnuke, but if you're able to write any javascript at all, then I don't understand why you're unable to call AJAX. Is it handling the server-side request that is the problem? (I am interested to know the reason, I am not criticising what you're saying)
|
0

you have to do the string concatenation.

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

1 Comment

Same as above. System.Web.HttpCompileException (0x80004005) ... error BC30201: Expression expected.
0

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

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

5 Comments

Did you add pageIndex++; var sPageIndex = pageIndex.toString; line above? And I guess you don't have to do "toString".
It makes my page fail completely. Even if I pass it as it with your edited version. Only if I pass it as '<%= xDoSomething(123456)%>' it works
@Sourabh- When you add a server tag <%= ... %> it becomes a server control then there is not need of adding <%= xDoSomething('+sPageIndex+')%> inverted commas in parameter unless you are concatenating something. <%= xDoSomething(sPageIndex)%> this one is the correct way to pass parameter to server function.
@Suprabhat, I'm interested to know how an inline block becomes a server control, please tell us more. I'm also interested to know how the client-side sPageIndex is used within the server-side inline block
@freefaller : Thanks for correction, i am pretty much aware of Inline Code Blocks but wasn't aware that i have made such kind of mistake while commenting. May be due to stressful day :( . Thanks for acknowledging my mistake :)
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.