Solved

Javascript alert from a vb class in the app_code folder

Posted on 2010-11-29
6
1,202 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

829 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