Solved

Executing Javascript on Page_Load()

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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…

747 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

13 Experts available now in Live!

Get 1:1 Help Now