Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Executing Javascript on Page_Load()

Posted on 2008-10-29
4
Medium Priority
?
405 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 400 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 1600 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

886 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