Solved

Javascript alert from a vb class in the app_code folder

Posted on 2010-11-29
6
1,184 Views
Last Modified: 2012-08-13
I developed a class in vb.net  that will be referenced in an ASP.net website. I will distribute the compiled class to my client for him to reference to in his website and call teh methods from his web pages. How can I incorporate in my class a javascript alert if the user encounters an error when calling the public methods that are exposed by my class. I am assuming that it has to do with referencing the httpContext from within the class but I am not sure how to do it. Any idea how to achieve this?
Thanks
0
Comment
Question by:qwerty360
6 Comments
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 166 total points
ID: 34235542
You can use Response.Write("<SCRIPT LANGUAGE=""JavaScript"">alert('Error encountered!!!')<" & "/" & "SCRIPT>")

More infos here : http://www.java2s.com/Code/ASP/Response/UsingtheResponsetooutputJavascriptcodeVBnet.htm
0
 
LVL 2

Assisted Solution

by:maymounlb
maymounlb earned 167 total points
ID: 34236137
place this sub in the class
    Public Sub JSAlert(ByVal clScript As System.Web.UI.ClientScriptManager, ByVal message As String)
        Dim alertScript As String = "<script type=""text/javascript"">"
        alertScript &= "window.alert('" & message & "');"
        alertScript &= "</script>"

        clScript.RegisterClientScriptBlock(Me.GetType(), "SCRIPTLOGOUT", alertScript)
    End Sub

and call it this way



            class1.JSAlert(ClientScript, "Test") from the pages
0
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 34236552
Pls Register Script to script manager as scriptblock or startup script.
0
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.

 
LVL 29

Assisted Solution

by:Kumaraswamy R
Kumaraswamy R earned 167 total points
ID: 34238204
0
 

Accepted Solution

by:
qwerty360 earned 0 total points
ID: 34238314
Neither of the above suggestions worked. I took your sugestions a step further and tried the following 2 possibilities but that did not work as well. Now remember that I am not putting code in the web.ui pages. All the code should be in the class. Any other suggestions? thanks.
Public Sub ErrorAlert1(ByVal errorMesage As String)

    Dim myScript As String = "<script type=""text/javascript"">"

    myScript &= "alert('" & errorMesage & "');"

    myScript &= "</script>"

    Dim page As Page = HttpContext.Current.CurrentHandler

    page.Response.Write(myScript)

End Sub





Public Sub ErrorAlert2(ByVal errorMesage As String)

    Dim myScript As String = "alert('" & errorMesage & "');"

    Dim page As Page = HttpContext.Current.CurrentHandler

    If (Not page.ClientScript.IsStartupScriptRegistered("myErrorAlert")) Then

      page.ClientScript.RegisterStartupScript(Me.GetType(), "myErrorAlert", myScript, True)

    End If

End Sub

Open in new window

0
 

Author Closing Comment

by:qwerty360
ID: 34276688
Both methods I am showing actually work. Thanks for your contribution.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
date format angularjs2 16 43
Problem to go to Web page 2 52
Understanding Trigger in Jquery 10 28
JavaScript Scope issue 4 20
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 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

11 Experts available now in Live!

Get 1:1 Help Now