Thursday, March 20, 2008

Calling Javascript Function from Code Behind (.vb)

This is the function at html page:

<script language="javascript">
   function test()
   {
      //Add the function code here
   }
</script>


To call this javascript function from code behind (.vb), Use Page.RegisterStartupScript function like the code below:

Dim strScript As String = "<script language='javascript' id='myClientScript'>test();</script>"
Page.RegisterStartupScript(“callTest”,strScript)

3 comments:

Anonymous said...

a simple yet helpful piece of code...tnx a lot.

Anonymous said...

I don't get it...when I enter the same code in file xxx.aspx.vb visual studio gives me an error. It doesn't take well "<-/-script>" even if it is inside double quotes. I can't figure out how come you guys dont have the same error.

Anonymous said...

Very good, a simple and very helpful code . . . thanks a lot