How Dynamically Insert Javascript into Asp.net

ASP.net provides you with some interesting blocks to do this.The following code shows how Dynamically insertjavascript in ASP.net page.


Dim sb as new StringBuilder
sb.append("")
Page.RegisterStartupScript("myScript", sb.toString)


This block will register the script on the page and then display a pop up window.

this script is used to create Java script message box from ASP.net page dynamically.

Public Class Utilities


Public Shared Sub CreateMessageAlert(ByRef aspxPage As System.Web.UI.Page, _
ByVal strMessage As String, ByVal strKey As String)
Dim strScript As String = "<script language=JavaScript>alert('" _
& strMessage & "')</script>"

If (Not aspxPage.ClientScript.IsStartupScriptRegistered(strKey)) Then
aspxPage.ClientScript.RegisterStartupScript(GetType(String), strKey, strScript)
End If
End Class

To display a message box from your code, you need to call the following line of code.

Utilities.CreateMessageAlert(Me, strMessage, "strKey1")


Comments

Author: Kapil Dhawan18 Jun 2008 Member Level: Gold   Points : 2

Hello
Nice piece of code
Thanks for sharing your knowledge with us.
I hope to see more good code from your side
This code will help lots of guys
Thanks to you
Regards,
Kapil



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: