การที่จะเขียน javascript ในฝั่ง code behide หรือ file .vb นั้นทำได้ไม่ยากมากวันนี้ขอยกเอาหลากหลายวิธีอาจจะไม่ทั้งหมดแต่เอาเท่าที่รู้ก็แล้วกัน
1. ง่ายสุดเป็นการแสดงออกโดยการ response.write เป็นการเขียนออกหน้าจอยกตัวอย่างเช่น
Response.Write("<script>alert('Hello Test');</script>"); 2. RegisterStartupScirpt
ตัวอย่างเช่น
Dim csm As ClientScriptManager = Page.ClientScript
If (Not csm.IsStartupScriptRegistered(Me.GetType(),
"ScriptKey1")) ThenDim str As String = "alert('Meesage from
RegisterStartupScript');"csm.RegisterStartupScript(Me.GetType(),
"ScriptKey1", str, True)End If
3. RegisterClientScriptBlock
If (Not csm.IsClientScriptBlockRegistered(Me.GetType(),
"ScriptKey2")) ThenDim str As New StringBuilder()
str.Append("<script type=text/javascript> ")
str.Append("alert('Meesage from RegisterClientScriptBlock'); </")
str.Append("script>")
csm.RegisterClientScriptBlock(Me.GetType(), "ScriptKey2",
str.ToString(), False)End If
ตัวอย่างเช่น ข้อแตกต่างกันระหว่าง RegisterStartupScirpt กับ RegisterClientScriptBlock
เมื่อทำการรัน code ทั้ง RegisterStartupScirpt และ RegisterClientScriptBlock จากตัวอย่าง code
<%@ Page Language="VB" %>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object,
ByVal e As System.EventArgs)
Dim csm As ClientScriptManager = Page.ClientScript
If (Not csm.IsStartupScriptRegistered(Me.GetType(),
"ScriptKey1")) Then
Dim str As String = "alert('Meesage from RegisterStartupScript');"
csm.RegisterStartupScript(Me.GetType(), "ScriptKey1", str, True)
End If
If (Not csm.IsClientScriptBlockRegistered
(Me.GetType(), "ScriptKey2")) Then
Dim str As New StringBuilder()
str.Append("<script type=text/javascript> ")
str.Append("alert('Meesage from
RegisterClientScriptBlock'); </")
str.Append("script>")
csm.RegisterClientScriptBlock(Me.GetType(),
"ScriptKey2", str.ToString(), False)
End If
End Sub
</script>
<html>
<head>
<title>Sample</title>
</head>
<body>
<form id="Form1"runat="server">
<!-- Message -->
</form>
</body>
</html>
ผล source code จากการรัน
<html>
<head>
<title>Sample</title>
</head>
<body>
<form name="Form1" method="post"
action="testscript.aspx" id="Form1">
<div>
<input type="hidden" name="__VIEWSTATE"
id="__VIEWSTATE" value="
/wEPDwUKLTUxMTcwNzgxMGRk//sVJuo7xvs64uz48zsgrX1nqX4=" />
</div><script type=text/javascript> alert('Meesage from
RegisterClientScriptBlock'); </script>
<!-- Message -->
<script type="text/javascript">
<!--
alert('Meesage from RegisterStartupScript');// -->
</script>
</form>
</body>
</html>
สังเกตุว่า ถ้าใช้ RegisterStartupScript Script จะปรากฎก่อนปิด </form> และ ถ้าใช้ RegisterClientScriptBlock script จะปรากฏหลังเปิด <form> การทำงานทุกอย่างเหมือนกันต่างกันตรงตำแหน่งในการแสดงผลเท่านั้น
3. การใช้ HtmlGenericControl เพื่อทำการเพิ่ม script ในตอนหัวของ html หรือ <head></head>
Dim script As HtmlGenericControl = New HtmlGenericControl
script.TagName = "script"
script.Attributes.Add("type", "text/javascript")
script.Attributes.Add("src", "filename.js")
Page.Header.Controls.Add(script)
อีกตัวอย่างหนึ่ง
Dim script As HtmlGenericControl = New HtmlGenericControl
script.TagName = "script"
script.Attributes.Add("type", "text/javascript")
script.InnerHtml = "alert('hello test');"
Page.Header.Controls.Add(script)