Inject JavaScript from code behind (C#/VB.NET)

C#

var sb = new System.Text.StringBuilder();sb.AppendLine("<script language='javascript'>");sb.AppendLine("var pageId = ' + PageId + ';");sb.AppendLine("</script>n");Type t = this.GetType();if (!Page.ClientScript.IsClientScriptBlockRegistered(t, ("ScriptName"))){    Page.ClientScript.RegisterClientScriptBlock(t, "ScriptName", sb.ToString());}

VB.NET

Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()sb.AppendLine("<script language='javascript'>")sb.AppendLine("var channelid = '" & PageID & "';")sb.AppendLine("</script>")Dim t As Type = Me.GetType()If Not Page.ClientScript.IsClientScriptBlockRegistered(t, ("ScriptName")) Then    Page.ClientScript.RegisterClientScriptBlock(t, "ScriptName", sb.ToString())End If

Leave a Reply

Your email address will not be published. Required fields are marked *