Link to home
Start Free TrialLog in
Avatar of Marcelo_Rodosis
Marcelo_Rodosis

asked on

Call a javascriptfunction in PageLoad event

How can I call a javascript function in my codebehind PageLoad event???
Avatar of tusharashah
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
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
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)
Avatar of Marcelo_Rodosis

ASKER

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....
ASKER CERTIFIED SOLUTION
Avatar of tusharashah
tusharashah

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, it works !!!
And I'm sufering because a sigle line of code!!!
hehehehhehe

thanks  tusharashah
you got the points
Nice to have you going Marcelo!

Thanks for A

-tushar