Solved

Javascript alert from a vb class in the app_code folder

Posted on 2010-11-29
6
1,191 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…

895 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

17 Experts available now in Live!

Get 1:1 Help Now