• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2152
  • Last Modified:

Call a javascriptfunction in PageLoad event

How can I call a javascript function in my codebehind PageLoad event???
0
Marcelo_Rodosis
Asked:
Marcelo_Rodosis
1 Solution
 
tusharashahCommented:
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
 
CJ_SCommented:
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
 
AerosSagaCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Marcelo_RodosisAuthor Commented:
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
 
tusharashahCommented:
Just add following code in your Page_Load

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

-tushar
0
 
Marcelo_RodosisAuthor Commented:
Yes, it works !!!
And I'm sufering because a sigle line of code!!!
hehehehhehe

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

Thanks for A

-tushar
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now