[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

asp.net vb.net Message box Java script error

Posted on 2011-03-03
8
Medium Priority
?
571 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
[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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 total points
ID: 35177150
0
 

Author Closing Comment

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

656 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