0

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.

2
Contributors
3
Replies
4
Views
11 Years
Discussion Span
Last Post by hollystyles
0
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.

0

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.

0

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>
<head>
<script type="text/javascript">
    function myFunction(data)
    {
        alert(data);
    }
</script>
</head>
<body>
</body>
</html>

ASP.NET page

string data = "Hello!";

string script = "<script type=\"text/javascript\">";
script += "myFunction('" + data + "');</script>";
Page.RegisterStartupScript("myscript", script);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.