PDA

View Full Version : Send values from script to c#



Rex Mundi
Jun 30th, 2005, 02:28 PM
Hi,

Can anyone tell me how to send values from a javascript back to c#.net code so they can be stored in an SQL database?

I have some code that allows the user to drag a DIV layer, and what i want to do is save the positions of the DIV's after they have been used

Rex

martin_narg
Jun 30th, 2005, 06:14 PM
You can do one of many things, the simplest would be to send an xmlHTTP call with the final div co-ords "behind the scenes" to C# which would in turn save the querystring to the database:
something like this:



<script>
var xmlhttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@end @*/
if(!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}

function sendDivCoords() {
var sURL = "myCSharpHandlerPage.cs?coords=" + parseInt(document.getElementById("myDiv").style.left) + "," + parseInt(document.getElementById("myDiv").style.top);

xmlhttp.open("GET", sURL, true);
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState==4) {
alert("co-ords sent to the page");
}
}
xmlhttp.send(null)
}
</script>


hope this concept idea helps.

m_n

Rex Mundi
Jul 1st, 2005, 12:30 PM
Cheers for the help,

Just one other question though, where would this be called from? Am i to call the Javascript function from the html code, or is the whole section to be placed within the c# code?

Also, i'm kinda unsure as to what var type the values are sent back as.

Thanks
Rex