Solved

Executing Javascript on Page_Load()

Posted on 2008-10-29
4
386 Views
Last Modified: 2012-06-27
Hi all,
I have written a simple javascript function which opens a popup window to show search resluts. At the moment the Javascript is executed off the click of a button but i was wondering if it was possible to call the javascript function in the Page_Load() so the popup appears automatically when the page is loaded. Also i don't want to coming up on every PostBack so it would have to be when Page.IsPostBack = false.

Thanks in advance.
-M3mph15
0
Comment
Question by:M3mph15
  • 2
4 Comments
 
LVL 16

Assisted Solution

by:Gyanendra Singh
Gyanendra Singh earned 100 total points
ID: 22838037
please find sample code

also refer this link
http://bunkerhollow.com/blogs/matt/archive/2008/06/15/asp-net-execute-javascript-on-page-load.aspx
public Sub Page_Load(Sender As Object,e As EventArgs)     
       ' Form the script to be registered at client side.
'** add if ( !ispostback) if you only want to execute once
         Dim scriptString As String  = "<script language=JavaScript> function DoClick() {"
             scriptString += "showMessage2.innerHTML='<h4>Welcome to Microsoft .NET!</h4>'}"
         scriptString += "function Page_Load(){ showMessage1.innerHTML="
         scriptString += "'<h4>RegisterStartupScript Example</h4>'}<"
         scriptString += "/"
         scriptString += "script>"
 
             If(Not Me.IsStartupScriptRegistered("Startup")) Then
        Me.RegisterStartupScript("Startup", scriptString)
             End If
    End Sub

Open in new window

0
 
LVL 44

Accepted Solution

by:
scrathcyboy earned 400 total points
ID: 22838210
<BODY onLoad="popupFunction();" onReload="javascript:return false;" >
0
 
LVL 6

Author Comment

by:M3mph15
ID: 22838225
Hi,
I tried what you have suggested. The Page Loads fine, no errors displayed but the window doesn't popup either. Here is my code:

Dim lhid As String = Request.QueryString("lhid")
lblLhId.Text = lhid
Dim scriptString As String = "<script type='text/javascript'> function showCon() {"
scriptString += "var obj_calwindow = window.open("
scriptString += "'ContactResults.aspx?type=con&lhid=' + " & lhid & " + '&contype=&condate=&loc=', 'Language', 'width=800,height='+'300'+"
scriptString += "',status=no,resizable=yes,top=200,left=200,dependent=yes,alwaysRaised=yes'"
scriptString += ");"
scriptString += "}<"
scriptString += "/"
scriptString += "script>"
If (Not ClientScript.IsStartupScriptRegistered("Startup")) Then
ClientScript.RegisterStartupScript(GetType(Page), "Startup", scriptString)
End If
 Had to Modify Me.ClientScript because i kept getting a message saying in was obsolete.
What am i doing wrong?
-M3mph15
0
 
LVL 6

Author Comment

by:M3mph15
ID: 22838381
scrathcyboy your solution worked great. nice and Simple Thanks.  
BondinASP thanks for your suggestion. I didn't get time to play around with it to make it work. I think i was doing something wrong.
-M3mph15
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question