Solved

MessageBox ClientScript

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

12 Experts available now in Live!

Get 1:1 Help Now