Solved

Javascript alert from a vb class in the app_code folder

Posted on 2010-11-29
6
1,197 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

776 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