Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MessageBox ClientScript

Posted on 2013-11-28
2
Medium Priority
?
602 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

715 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