Call javascript method from asp.net code behind

Sometime I found it more efficient to run JavaScript on asp.net rather than doing everything on VB/c#.

Hope do you call a JavaScript by using the code behind aspx?

You can use the following code

Page.ClientScript.RegisterStartupScript(Me.GetType, “Key name”, “Javascript that you would like to use”, ‘Add script tag)

Here it is an example (“Hello OurChiWai”)

VB.net
Page.ClientScript.RegisterStartupScript(Me.GetType, “MyJS”, “alert(‘Hello OurChiWai’);”, true)

C#
Page.ClientScript.RegisterStartupScript(this.GetType, “MyJS”, “alert(‘Hello OurChiWai’);”, true);

‘I called my Key “MyJS”
The JavaScript that I am going to run is to do a message box which shows “Hello OurChiWai”

Happy coding.

Leave a Reply