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.

Recommended Answers

All 3 Replies

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.

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.

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);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.