How to call a javascript function from Asp.Net code?

LiLo

Freshman
Joined
Mar 10, 2006
Messages
33
Hi,

How do I call a javascript function from ASP.Net (VB) code?

I added a javascript file into the asp.net project and wrote some javascript statements to display an IFrame.

The javascript function in the jscript file is:

<script language = "javascript">

function showIFrame()
{
var iframe = document.createElement("iframe");

iframe.id = "testiframe";
iframe.name = "testiframe";
iframe.frameborder = 1;
iframe.height = 185;
iframe.width = 175;
iframe.src = "C:\Documents and Settings\RadioButtons\Office.jpg";
window.document.body.appendChild(iframe);

return false;

}

How do I call this function from ASP.Net(VB) code? :)
e.g Form1_Load(.....)
 
Hi,

You have to register a startup script using Page.RegisterStartupScript.
This is how I show alert boxes from my asp.net code.

Sub ShowMessage(message As String)
Dim sb As New System.Text.StringBuilder("")
With sb
.Append("<script language='JavaScript'>")
.Append("ShowMessage(""" & message & """);")
.Append("</script")
.Append(">")
End With
Page.RegisterStartupScript("ShowMessage", sb.ToString())
End Sub

<script language="javascript">
<!--
function ShowMessage(text) {
alert(text);
return false;
} //-->
</script>

Hope this helps.
 
Why, why, why? Why would you call it from form load as opposed to the onload event of the document? You are not passing in any information, it's pointless.
 
Thanks both for the replies :)

I used the form_load method because the javascript has to execute when the page loads up.

Today I just discovered that there's a Page_Load method also.

so what is the difference between form and page load methods for an asp.net webpage?
 
Look

<html>
<body onload="doMe();">
<script type="text/javascript">
function doMe()
{
alert("Yo");
}
</script>
</body>
</html>
 
Back
Top