LiLo Posted April 7, 2006 Posted April 7, 2006 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(.....) Quote
Polar Bear Posted April 7, 2006 Posted April 7, 2006 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. Quote
Diesel Posted April 7, 2006 Posted April 7, 2006 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. Quote
LiLo Posted April 10, 2006 Author Posted April 10, 2006 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? Quote
Diesel Posted April 10, 2006 Posted April 10, 2006 Look <html> <body onload="doMe();"> <script type="text/javascript"> function doMe() { alert("Yo"); } </script> </body> </html> Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.