DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   ASP.NET (http://www.daniweb.com/forums/forum18.html)
-   -   transfer data from an asp .net page into a javasscript script (http://www.daniweb.com/forums/thread51205.html)

sergiofmolina Jul 28th, 2006 10:50 am
transfer data from an asp .net page into a javasscript script
 
Hello, I am new at this forum. I am trying to figure out how, if possible at all, to transfer data from an asp .net page into a javascript script in an HTML page.

hollystyles Jul 28th, 2006 1:14 pm
Re: transfer data from an asp .net page into a javasscript script
 
string script = "<script type=\"text\\javascript\">function(){alert('some data');};</script>";
Page.RegisterClientScriptBlock("myscript", script);

That's an easy way. There are many others like AJAX google for that.

sergiofmolina Aug 1st, 2006 1:03 pm
Re: transfer data from an asp .net page into a javasscript script
 
Thanks hollystyles, I tried the code and did not work. It seems that this code works when the script and the data are in the same page. I have the script in an html page different to the asp page.

hollystyles Aug 2nd, 2006 10:15 am
Re: transfer data from an asp .net page into a javasscript script
 
There is more than one way to register the script. Maybe RegisterStartupScript may work better as it waits till the page is fully loaded in the browser before firing. Also I had my slash the wrong way around in the "text/javascript" attribute of the script tags.

HTML Page:

[HTML]<html>
<head>
<script type="text/javascript">
function myFunction(data)
{
alert(data);
}
</script>
</head>
<body>
</body>
</html>[/HTML]

ASP.NET page

string data = "Hello!";

string script = "<script type=\"text/javascript\">";
script += "myFunction('" + data + "');</script>";
Page.RegisterStartupScript("myscript", script);


All times are GMT -4. The time now is 6:02 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC