Solved

MessageBox ClientScript

Posted on 2013-11-28
2
569 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now