Solved

Call a javascriptfunction in PageLoad event

Posted on 2004-10-26
2,129 Views
Last Modified: 2008-02-01
How can I call a javascript function in my codebehind PageLoad event???
0
Question by:Marcelo_Rodosis
    7 Comments
     
    LVL 18

    Expert Comment

    by:tusharashah
    You can use RegisterClientScriptBlock for this...

    Sub Page_Load( sender as Object,e as EventArgs)
             
           'Form the script that is to be registered at client side.
            Dim scriptString as String = "<script language=JavaScript> "
            scriptString += "alert('Welcome to Microsoft .NET')<"
            scriptString += "/"
            scriptString += "script>"
         
                If(Not IsClientScriptBlockRegistered("clientScript"))
                  RegisterClientScriptBlock("clientScript", scriptString)
                End If
         End Sub

    OR

    You can simply call the function using <body onLoad="YourFunctionName()">

    -tushar
    0
     
    LVL 22

    Expert Comment

    by:CJ_S
    To be more specific you will need to use the

    RegisterStartupScript and IsStartupScriptRegistered

    Sub Page_Load( sender as Object,e as EventArgs)
             
           'Form the script that is to be registered at client side.
            Dim scriptString as String = "<script language=JavaScript> "
            scriptString += "alert('Welcome to Microsoft .NET')<"
            scriptString += "/"
            scriptString += "script>"
         
                If(Not IsStartupScriptRegistered("clientScript"))
                  RegisterStartupScript ("clientScript", scriptString)
                End If
    End Sub
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    Dim popupScript As String = "<script language='javascript'>" & _
      "window.open('PopUp.aspx', 'CustomPopUp', " & _
      "'width=200, height=200, menubar=yes, resizable=no')" & _
      "</script>"

    Page.RegisterStartupScript("PopupScript", popupScript)
    0
     

    Author Comment

    by:Marcelo_Rodosis
    I think i should be more clear. My problem is: I have a javacsript function:

    function myFunction() {
    .
    .
    .
    }

    and in pageload event i need to call this function, as something like this:

    Response.Write("javascript:myFunction()")

    but it doesn't work, and I have tried I lot of alternatives....
    0
     
    LVL 18

    Accepted Solution

    by:
    Just add following code in your Page_Load

    Page.RegisterStartupScript("Script", "<script>myFunction();</script>");

    -tushar
    0
     

    Author Comment

    by:Marcelo_Rodosis
    Yes, it works !!!
    And I'm sufering because a sigle line of code!!!
    hehehehhehe

    thanks  tusharashah
    you got the points
    0
     
    LVL 18

    Expert Comment

    by:tusharashah
    Nice to have you going Marcelo!

    Thanks for A

    -tushar
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Learn The Basics of Ethical Hacking & Pen Testing

    Computer and network security is one of the fastest growing and most essential industries in technology, meaning companies will pay big bucks for ethical hackers. This is the perfect course to leap into this lucrative career, learning how to use ethical hacking to reveal ...

    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    This video discusses moving either the default database or any database to a new volume.

    846 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

    10 Experts available now in Live!

    Get 1:1 Help Now