I have created a function called test that returns a boolean in a separate js file and I could call this function inside and aspx.vb file in vb.net as such:
ScriptManager.RegisterStartupScript(Me, Page.GetType, "Script", "test();", True)
my question is how to capture this returned value and assign it to a variable in vb.net so I can use it in a vb conditional statement?
CodePudding user response:
Actually you aren't calling the function. What RegisterStartupScript does is that it "tells" to the browser that it should run the test() function as soon as it's ready to do so.
With that been said, you cannot get a return value from the test() function because when it gets called your aspx vb code has already been executed. Take a look here for more information regarding the life cycle of an aspx page.
Usually it is not a good idea to write server side code (aspx) that depends on the result of client side code (js), however what you can do is to have the test() js function to do an ajax request in the aspx page and "go on" from there. Here is a small example. The thing is that this approach isn't suitable nor it's recommended for what, I suspect, you are trying to do. To present different web page parts to the user according to the output of the test() js function.
I cannot provide sample code for what I suggest because it will be a wrong use case. I am answering this just to emphasize that you actually can't do what are you asking and even if there is a way to do it you should definitely avoid it. However, this is a good opportunity to dive in more in the web applications design and implementation.
Perhaps an SPA can fit your needs?