Solved

asp.net vb.net Message box Java script error

Posted on 2011-03-03
8
559 Views
Last Modified: 2012-06-27
I have an asp.net web app with vb.net code. I am using a sub in a class to present a message box.  This is my first go at this and can't seem to get it to work.  I get the following error
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Thanks for the hlpe
******Code behind******
Protected Sub btnRun_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRun.Click

        WebMsgBox.ShowMessage("this is a test")
  End Sub
End Class

*****Class*****
Public Class WebMsgBox
    Protected Shared handlerPages As New Hashtable()
    Private Sub New()
    End Sub

    Public Shared Sub ShowMessage(ByVal Message As String)
        If Not (handlerPages.Contains(HttpContext.Current.Handler)) Then
            Dim currentPage As Page = DirectCast(HttpContext.Current.Handler, Page)
            If Not ((currentPage Is Nothing)) Then
                Dim messageQueue As New Queue()
                messageQueue.Enqueue(Message)
                handlerPages.Add(HttpContext.Current.Handler, messageQueue)
                AddHandler currentPage.Unload, New EventHandler(AddressOf CurrentPageUnload)
            End If
        Else
            Dim queue As Queue = DirectCast(handlerPages(HttpContext.Current.Handler), Queue)
            queue.Enqueue(Message)
        End If
    End Sub

    Private Shared Sub CurrentPageUnload(ByVal sender As Object, ByVal e As EventArgs)
        Dim queue As Queue = DirectCast(handlerPages(HttpContext.Current.Handler), Queue)
        If queue IsNot Nothing Then
            Dim builder As New StringBuilder()
            Dim iMsgCount As Integer = queue.Count
            builder.Append("<script language='javascript'>")
            Dim sMsg As String
            While (iMsgCount > 0)
                iMsgCount = iMsgCount - 1
                sMsg = System.Convert.ToString(queue.Dequeue())
                sMsg = sMsg.Replace("""", "'")
                builder.Append("alert( """ & sMsg & """ );")
            End While
            builder.Append("</script>")
            handlerPages.Remove(HttpContext.Current.Handler)
            HttpContext.Current.Response.Write(builder.ToString())
        End If
    End Sub
End Class

Open in new window

0
Comment
Question by:NCSA SCADA
8 Comments
 

Author Comment

by:NCSA SCADA
ID: 35032480
I have tried this another way and still get an error

****** VB Code *****
 If txtUserInit.Text = "" Then
            ASPNET_MsgBox("Blank")
End If

Public Sub ASPNET_MsgBox(ByVal Message As String)

        System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf)

        System.Web.HttpContext.Current.Response.Write("alert(""" & Message & """)" & vbCrLf)

        System.Web.HttpContext.Current.Response.Write("</SCRIPT>")

    End Sub

Open in new window

0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 35032904
See the code below : The first parameter into the call to CreateMessagealert is the Page object, 2nd is The message itself, the 3rd is a key to register the script with
Protected Sub btnRun_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRun.Click 
 
        CreateMessageAlert(Page, "this is a test", "AlertMessage") 
  End Sub 
End Class 


    Public Shared Sub CreateMessageAlert(ByRef aspxPage As System.Web.UI.Page, _
                                        ByVal strMessage As String, _
                                        ByVal strKey As String)
        Try
            Dim strScript As String = "<script language=JavaScript>alert('" & strMessage & "')</script>"
            Dim cs As ClientScriptManager = aspxPage.ClientScript
            If (Not cs.IsStartupScriptRegistered(strKey)) Then
                cs.RegisterStartupScript(aspxPage.GetType(), strKey, strScript)
            End If
        Catch ex As Exception
            Throw
        End Try
    End Sub

Open in new window

0
 

Author Comment

by:NCSA SCADA
ID: 35033689
something strainge is going on.  If I build a new project my second attempt and your example (jacko72) both works just fine.  I am including my asp code as well. I must have somthing setup wrong.

Thanks again
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="UserManagment.aspx.vb" Inherits="DBAPage.UserManagment" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <style type="text/css">

        .style1
        {
            width: 100%;
        }
        .style6
        {
            width: 302px;
            font-weight: bold;
        }
        .style8
        {
            width: 179px;
        }
        .style7
        {
            font-size: medium;
        }
        .style2
        {
            width: 210px;
            font-family: "Segoe UI";
            font-weight: bold;
            font-size: small;
        }
        .style10
        {
            font-size: small;
            font-weight: bold;
        }
        .style9
        {
            font-family: "Segoe UI";
            font-weight: bold;
            font-size: small;
        }
        </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:ScriptManager ID="ScriptManager1" AsyncPostBackTimeOut="36000" runat="server" />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <br />
            <br />
            <table class="style1">
                <tr>
                    <td>
                        <b style="text-decoration: underline; font-family: 'Segoe UI'; font-size: x-large;">User Information</b></span>
                    </td>
                    <td class="style8">
                        <span class="style7"></span>
                    </td>
                    <td style="font-size: x-large; font-family: 'Segoe UI'">
                        <b style="text-decoration: underline; font-family: 'Segoe UI'; font-size: x-large;">Selections & Server Information</b></span>
                    </td>
                   
                </tr>
                <tr>
                    <td class="style2">
                            Please Enter Users Initials:&nbsp;&nbsp;</td>
                    <td class="style8">
                        <asp:TextBox ID="txtUserInit" runat="server" height="22px" width="128px"></asp:TextBox>
                    </td>
                    <td>
                        <span class="style1"><span class="style6"><b style="font-size: small">&nbsp;Please select the correct operation</b></span></span></td>
                </tr>
                <tr>
                    <td class="style2">
                            Please Enter Users First Name:&nbsp;&nbsp;</td>
                    <td class="style8">
                        <asp:TextBox ID="txtFirstName" runat="server" height="22px" width="128px"></asp:TextBox>
                    </td>
                    <td>
                        <span class="style1"><span class="style6"><b style="font-size: small">
                        <asp:RadioButton ID="RadioButton1" runat="server" 
                                GroupName="MyGroup" Text="Create VOB User" Checked="True" />
                            &nbsp;<asp:RadioButton ID="RadioButton2" runat="server" GroupName="MyGroup" 
                                Text="Disable VOB User" />
                            &nbsp;<asp:RadioButton ID="RadioButton3" runat="server" GroupName="MyGroup" 
                                Text="Create Windows User" />
                            &nbsp;<asp:RadioButton ID="RadioButton4" runat="server" GroupName="MyGroup" 
                                Text="Disable Windows User" />
                        </b></span></span>
                    </td>
                </tr>
                <tr>
                    <td class="style2">
                            Please Enter Users Last Name:&nbsp;&nbsp;</td>
                    <td class="style8">
                        <asp:TextBox ID="txtSurName" runat="server" height="22px" width="128px"></asp:TextBox>
                    </td>
                    <td>
                            &nbsp;</td>
                </tr>
                <tr>
                    <td class="style2">
                            Please Enter Users Organization:</td>
                    <td class="style8">
                        <asp:TextBox ID="txtOrganization" runat="server" height="22px" width="128px"></asp:TextBox>
                    </td>
                    <td>
                            &nbsp;<span class="style1"><span class="style6"><b style="font-size: small">&nbsp;<span 
                                class="style10">Please select the correct s</span><span class="style9">e</span><span 
                                class="style10">rver option</span></b></span></span></td>
                </tr>
                <tr>
                    <td class="style2">
                            Please Enter Users Email Address:</td>
                    <td class="style8">
                        <asp:TextBox ID="txtEmail" runat="server" height="22px" width="128px"></asp:TextBox>
                    </td>
                    <td>
                        <span class="style1"><span class="style6"><b style="font-size: small">
                        <asp:RadioButton ID="RadioButton5" runat="server" GroupName="MyGroup2" 
                                Text="One Server" Checked="True" />
                            &nbsp;<asp:RadioButton ID="RadioButton6" runat="server" GroupName="MyGroup2" 
                                Text="All Servers" />
                        </b></span></span>
                    </td>
                </tr>
                <tr>
                    <td class="style2">
                            Please Enter Users Password:</td>
                    <td class="style8">
                        <asp:TextBox ID="txtPass1" runat="server" height="22px" width="128px"></asp:TextBox>
                    </td>
                    <td>
                        <asp:TextBox ID="txtServerIP" runat="server" height="22px" 
                                width="128px"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="style2">
                            Please Confirm Password:</td>
                    <td class="style8">
                        <asp:TextBox ID="txtPass2" runat="server" height="22px" width="128px"></asp:TextBox>
                    </td>
                    <td>
                            &nbsp;</td>
                </tr>
            </table>
            <hr />
            <span class="style1"><span class="style6"><b style="font-size: small">
            <br />
            </b>
            <asp:Button ID="btnRun" runat="server" Text="Run" />
            &nbsp; </span>
            <asp:UpdateProgress ID="UpdateProgress1" runat="server" 
                        AssociatedUpdatePanelID="UpdatePanel1">
                <ProgressTemplate>
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <asp:Image ID="Image1" runat="server" 
                                style="text-align: center" ImageUrl="../PICS/Turbine3.gif" Height="28px" />
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Executing Query......Please Wait!!<p>
                </ProgressTemplate>
            </asp:UpdateProgress>
            <asp:GridView ID="GridView1" runat="server">
            </asp:GridView>
            </p>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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 8

Expert Comment

by:Anurag Agarwal
ID: 35034516
ssconce u can also try this for WebMsgBox

 Sub WebMsgBox(ByVal current_page As Page, ByVal message As String) 
        current_page.ClientScript.RegisterStartupScript(current_page.GetType, "WebMsgBox", "window.alert('" & message & "');", True)
           End Sub

Open in new window

0
 
LVL 8

Expert Comment

by:Anurag Agarwal
ID: 35034632
little updation ...... since u have used update pannel use this instead

 Sub WebMsgBox(ByVal current_page As Page, ByVal message As String) 
        current_page.ClientScript.RegisterStartupScript(current_page, current_page.GetType, "WebMsgBox", "window.alert('" & message & "');", True)
           End Sub

Open in new window


for more refereence check RegisterStartupScript
0
 
LVL 7

Expert Comment

by:foobarr
ID: 35061916
Have you just tried creating an ASP Literal Control?

You could go in markup
<asp:Literal Id="Literal1" runat="server" />

In code behind
then after builder.Append("</script>")

Literal1.Text = builder.toString()
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 35177150
0
 

Author Closing Comment

by:NCSA SCADA
ID: 35327390
Thank you - that worked great
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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