Solved

Executing Javascript on Page_Load()

Posted on 2008-10-29
4
379 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Regular Expression Calculator Tester 2 53
Convert an ASPX page into PDF 7 22
Exit the loop 4 34
Redirect Website for Mobile Devises 7 21
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

919 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now