Can VB.NET access javascript variables?

Hi,

I’ve populated an array in javascript and on submitting my form I need to access this javascript variable in my vb.net code. Is this possible and if so how?

Any help gratefully received!

no it is not possible in vb.net to access javascript code.
But you can send that array in hidden form field, By assigning that array to the field value property by using some saprator.
here is the server side code


for (i=0; i<document.getElementById('lstSelectedStores').options.length; i++)
	{
	//alert('location is selected');
		//document.getElementById('selectedLocations').value += "^"
		document.getElementById('selectedLocations').value +=document.forms[0].lstSelectedStores.options[i].value + ",";
	}
	

Here is html code


<input id="selectedLocations" type="hidden" size="1" runat="server">

now on server you can get the values of the javascript array…
Enjoy

Thanks very much mrsaif, that looks like it will work! :slight_smile:

Isn’t it a major disappointment that asp.net developers have to resort to such “dirty” methods to do something we should be able to do within asp.net? Do PHP developers have the same problem? Sorry, I’m getting off track!

Yes, PHP developers, RoR developers, ASP developers, JSP developers, CGI/Perl/Python developers. We are all in the same boat: The browser is separate from the server, and it works disconnected.

Ok, that’s interesting - thanks.

Is writing to a hidden input field a standard workaround? I don’t come from a web programming background (no kidding), so all this is really useful for me to learn.

Well, I personally wouldn’t do it that way, because the user can change the value if they so wished. So if it is something valuable, or something that should not be changed, you probably will not want to go about it in this way.

As Benjymouse said, all web languages have this same, I dare not call it issue. It’s inevitable. Javascript runs on the user’s browser, thus why you can access elements on the page, handle events on the page in realtime, etc. However, ASP.NET runs on the server, and so cannot access the user’s browser in such a fashion.

The best way to have client and server side interaction, in my opinion, is through AJAX, of course dependent on the situation. Javascript can send values to ASP.NET, and the application can respond by sending values back. For such functionality, you would want to take a look at Atlas, which is a library for ASP.NET to make it a lot easier to develop AJAX functionality.

I’m not saying whether or not this is the best solution for your situation, but it’s simply a suggestion. :slight_smile:

I’m not sure whether atlas is the way to go on my problem (but maybe it is). The user clicks on elements on the screen to define their sequence, and on clicking a button I need to send this sequence, held in an array, up to the server. I’ll nead to do a bit more research on atlas.

Thanks devbanana.

I don’t know. The neat thing about Atlas is that you can consume web services with it, and it has support for passing complex types to the function. I don’t know if this includes arrays or not. But personally, this is the way I would go.