Solved

MessageBox ClientScript

Posted on 2013-11-28
2
588 Views
Last Modified: 2013-11-29
Needed a messagebox that worked in asp.net and found this example which fits perfectly. On the form itself works fine. But i want create a function to call from any form.

Original Code

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim message As String = "Hello! Mudassar."
    Dim sb As New System.Text.StringBuilder()
    sb.Append("<script type = 'text/javascript'>")
    sb.Append("window.onload=function(){")
    sb.Append("alert('")
    sb.Append(message)
    sb.Append("')};")
    sb.Append("</script>")
    ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", sb.ToString())
End Sub

Open in new window


My Code

I try to pass the message and webform (I think that is how a form is passed to a function). But in ClientScript.RegisterClientScriptBlock, an error message appears "clientscript is not declared"
 Public Shared Function MyMsgbox(ByVal Msg As String, ByVal WebFrm As Page) As String
        MyMsgbox = ""
        Dim message As String = msg
        Dim sb As New System.Text.StringBuilder()
        sb.Append("<script type = 'text/javascript'>")
        sb.Append("window.onload=function(){")
        sb.Append("alert('")
        sb.Append(message)
        sb.Append("')};")
        sb.Append("</script>")
        ClientScript.RegisterClientScriptBlock(WebFrm.GetType(), "alert", sb.ToString())
    End Function

Open in new window

0
Comment
Question by:rflorencio
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
ID: 39684261
The ClientScript identifier is a property of the Page object, so just put WebFrm. before it.

Side notes: As you are not returning anything useful, your method should be a Sub. You should escape backslashes and apostrophes in the message string, so that the client code doesn't crash if you happen to put those in the message. You can use the overload of RegisterClientScriptBlock that adds script tags, so that you don't have to put them in the string.

Public Shared Sub MyMsgbox(ByVal Msg As String, ByVal WebFrm As Page)
        Dim sb As New System.Text.StringBuilder()
        sb.Append("window.onload=function(){")
        sb.Append("alert('")
        sb.Append(Msg.Replace("\", "\\").Replace("'", "\'"))
        sb.Append("')};")
        WebFrm.ClientScript.RegisterClientScriptBlock(WebFrm.GetType(), "alert", sb.ToString(), True)
    End Function

Open in new window

0
 

Author Closing Comment

by:rflorencio
ID: 39684822
excellent
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question