Dynamically inserting javascript in ASP.net page if often not known to developers. I have spent a lot of time figuring out this, when I was pretty new to dot net. ASP.net provides you with some interesting blocks to do this. below is the code which will help you do this.
Dim sb as new StringBuilder
sb.append("<script language=javascript>")
sb.append("window.open('myPage.aspx', 'CustomPopUp', ")
sb.append("'width=200, height=200, menubar=yes, resizable=no')")
sb.append("</script>")
Page.RegisterStartupScript("myScript", sb.toString)
This block will register the script on the page and then display a pop up window.
I was surfing to find some interesting code about javascript in ASP.net, and saw the following code block. I am not able to recollect where I saw this script, but it really is interesting 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")
I hope this helps.
No comments:
Post a Comment