Solved

MessageBox ClientScript

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 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