How to Include JavaScript Between Vb.NET

Integrating JavaScript into your Visual Basic .NET (VB.NET) code lets you use both programming languages in a single function. This helps you create dynamic web content for your website with an ASP.NET page. Including JavaScript code in a VB.NET function is done by creating a "ClientScriptManager" object that manages scripts and its "RegisterStartupScript" method, which registers a specific script to the page.

Instructions

    • 1

      Open your ASP file in an editor such as Windows Notepad.

    • 2

      Set the page language to "VB" and specify the "DOCTYPE" by adding the following code at the top of your file:

      <%@ Page Language="VB" %>

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    • 3

      Create a VB subroutine which uses JavaScript code through use of the "RegisterStartupScript" method by adding the following code after the "DOCTYPE" line:

      <script runat="server">

      Public Sub Page_Load(ByVal sender As [Object], ByVal e As EventArgs)

      Dim cs_name As [String] = "Testing JavaScript"

      Dim cs_type As Type = Me.[GetType]()

      Dim csm As ClientScriptManager = Page.ClientScript

      If Not csm.IsStartupScriptRegistered(cs_type, cs_name) Then

      Dim cs_text As New StringBuilder()

      cs_text.Append("<script type=text/javascript> alert('Using JavaScript!') </")

      cs_text.Append("script>")

      csm.RegisterStartupScript(cs_type, cs_name, cs_text.ToString())

      End If

      End Sub

      </script>

    • 4

      Add the HTML code for the rest of your Web page, save the file and load it on your server to execute the JavaScript code.

Related Searches:

References

Comments

Related Ads

Featured