Saturday, March 24, 2012

passing variables from javascript to ASP code

Hi there,

I was wondering if anyone can tell me a way of passing variables through a javascript function, to a visual basic function, by any means.

However I would like to be able to do this without reloading the page, for aesthetics sakes

cheers Neilhello:

You can't pass javascript variables to ASP functions. The ASP functions run server-side and have completed their execution once the page has loaded and the javascript is ready to be run.

Also, you might do the following:

It's easy to use the value of a VBScript variable in java_script_:
document.writeln('<%= MyVBScriptVariable %>');

Or here is an example which puts the value of a VBScript variable into a JavaScript alert box:
Response.Write "<script language='JavaScript'>alert('The value is " & MyVBScriptVariable & "');</script>"

But to send the value of a JavaScript variable to VBScript, well that's another story. You could have the JavaScript fill in a hidden form field and then post the form. That way VBScript can pick up the value of the hidden form field with Request.Form("MyFieldName").

try any of these.

regards !!!!
Depending on what your application needs to do with the variables, you could use an IFRAME and have the javascript function submit a form or call a url with a querystring in the IFRAME. Resulting page can return any results you need to use to update client side controls on your page.

Also, if your pages are taking too long to load on postbacks make sure you have SmartNavigation set to true in your page.

Hope this helps.
Hello!

You can use Web services from JavaScript, so write a Web service(th actual function on VB) and call it from JavaScript with parameter you want to pass to VB function.

Hope this helps
I'm not sure of the specifics of what you are trying to do, but to pass strings to code-behind functions, all you have to do is set the html web controls to runat=server. For instance, we use a javascript popup window(widow.open(page.html,windowname,location)) to retrieve comments on our aspx pages. We use a hidden form control(set runat=server) to retrieve the information when the page is posted back. Of course this probably won't fit your need, but you can do it.
in the end i put variables into a js function call in a template on a datalist, then saved the variable in a cookie, which I then requested from the vb code.

thanks for the help though i will look at this post later to take it in properly :)

0 comments:

Post a Comment