Link to home
Start Free TrialLog in
Avatar of g_johnson
g_johnsonFlag for United States of America

asked on

Why doesn't this alert using java in ASPX work?

Hi.

I have this code in an ASPX page:

        Dim b As Boolean = rept.ReportProduction()
       
        If b = False Then
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "Key1", "showDialogue('" & rept.errMess & ".')", True)
        End If


    <script type="text/javascript" language="javascript">

        function showDialogue(message) { alert(message); }

    </script>

The above does not work.

However, this does pop the alert:
        Dim b As Boolean = rept.ReportProduction()
       
        If b = False Then
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "Key1", "showDialogue('" & "junk" & ".')", True)
        End If

In short, if I use a variable it doesn't work; if I just put in text it does work.

Can it be made to work with a variable?
Avatar of Randy Poole
Randy Poole
Flag of United States of America image

Best guess rept.errMess is null
Avatar of g_johnson

ASKER

Sorry -- I should have made that clear.  rept.errMess is a rather long string:

Error in ReportProduction: 45: 80: Could not Report Production: Imitmidx.Read:Item Number/Location not found: FORK / MA
Avatar of David Johnson, CD
Use regular c# when javascript doesn't work


Webform1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication18
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)  { }
        
        public void ErrorTrap(string str) {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alert" + UniqueID, "alert('" + str + "');", true);
        }

protected void Button1_Click1(object sender, EventArgs e)
        {
            ErrorTrap(TextBox1.Text);
            TextBox1.Text = "";
        }
    }
}

Open in new window

WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication18.WebForm1" %>
<!DOCTYPE html>
<html4f>
<head runat="server">
    <title>Alert Box</title>
    <style type="text/css">
        .auto-style1 {
            text-align: left;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="auto-style1">
            <div class="auto-style1"><asp:TextBox ID="TextBox1" runat="server" Height="30px" Width="345px"></asp:TextBox></div>
            <div class="auto-style1">
            <br />

            </div>

       <div>
         <p class="auto-style1">&nbsp;<asp:Button ID="Button1" runat="server" Text="Click To Show Text in Alert" OnClick="Button1_Click1" style="margin-bottom: 0px" Width="354px"></asp:Button>/</div>
    </form>
</body>
</html>

Open in new window


with help from http://bit.ly/ZnblOK
That also is not working.  If I just type in something like "junk" instead of my variable, it works, but if I use my variable it does not work.  I'm working in VB, not C#, but I was able to translate your code to see the results.
ASKER CERTIFIED SOLUTION
Avatar of g_johnson
g_johnson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
perhaps your variable is not in scope
This turns out to be the real issue.  There was nothing wrong with the code itself.