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

x
?
Solved

Javascript alert from a vb class in the app_code folder

Posted on 2010-11-29
6
Medium Priority
?
1,240 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
[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
6 Comments
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 664 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 668 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 29

Assisted Solution

by:Kumaraswamy R
Kumaraswamy R earned 668 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…

660 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