ASP.NET: Register a javascript function
From Wiki
Summary: Register a javascript function
There are a couple of ways to register javascript from your ASP.NET code behind pages. Firstly, if you want to register a function piece of javascript code that will run when the page loads, you can use the ClientScript.RegisterStartupScript method. Here's a simple example:
- ClientScript.RegisterStartupScript(Me.GetType(), "myjavascript", "alert('hello');", True)
Secondly, you may just want to register a function on the page so that other methods can call it. This is where the ClientScript.RegisterClientScriptBlock method comes in:
- Dim myFunction As New StringBuilder
- myFunction.AppendLine("function callMe() {")
- myFunction.AppendLine("alert('hello');")
- myFunction.AppendLine("}")
- ClientScript.RegisterClientScriptBlock(Me.GetType(), "myjavascript", myFunction.ToString, True)
In the above example, you can now call the javascript "callMe" function from other elements on your page.
This Hack is part of the ASP.NET Hacks collection


