PDA

View Full Version : how to create an alert box in ASP


debbie_lee104
07-19-2007, 03:56 PM
Hi i'm trying to create an alert box. I dont want to execute a javascript at a button pression, but only if a condition is true. This is my aspx.vb file:

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
'-----------------------------------------------------------------------
'Description: This function executes when the Cancel button is clicked on. It cancels
' the pending file(s) that are currently checked.
'
'Inputs: System Controlled
'
'Ouputs: None
'
'Return Code: None
'-----------------------------------------------------------------------
Dim MyTable As HtmlTable = Page.FindControl("tabJobList")
Dim MyRows As HtmlTableRow
Dim MyCheckBox As HtmlInputCheckBox
Dim Cnt As Integer
Dim JobID As Integer
Dim CancelSuccess As Boolean = False
Dim OutParms As DictionaryEntry
Dim MyDR As SqlClient.SqlDataReader 'declare sql server connection
Dim CancelQuery As New PCMS_Services.DatabaseAccess
Dim MyLog As PCMS_Services.Log = New PCMS_Services.Log

Try
For Cnt = 1 To MyTable.Rows.Count - 1
MyRows = MyTable.Rows(Cnt)
If MyRows.Cells(0).Controls.Item(0).ID <> Nothing Then
MyCheckBox = MyRows.Cells(0).Controls(0)
If MyCheckBox.Checked = True Then
JobID = MyRows.Cells(1).InnerText()
'**************************************************
' Ensure we have an open connection to the database. Pass in the name of the
' database this user is currently accessing to the query.
' Make connection to the global database and if the query is successful,
' cancel job from job list
'**************************************************
CancelQuery = New PCMS_Services.DatabaseAccess
CancelQuery.Open(Session("DB_UserName"), Session("DB_Password"), Session("DB_ActServer"), Session("DB_Actuals"))
CancelQuery.ClearParms()
CancelQuery.AddParameter("@InJob", JobID, ParameterDirection.Input, SqlDbType.Int, 4)
CancelQuery.AddParameter("@InUser", Session("LoggedOnUser"))
CancelQuery.AddParameter("@RetCount", -1, ParameterDirection.Output, SqlDbType.Int, 4)
MyDR = CancelQuery.ExecuteCommand("[dbo.ADW_CancelPendingJobs",PCMS_Services.DatabaseAccess.ExecuteType.Reader, CommandType.StoredProcedure)
'**************************************************
' If stored procedure returns <> 1 , notify user that the job is Active and cannot be cancelled
'**************************************************
If MyDR.RecordsAffected = 1 Then '1 indicates failure to cancel
MsgBox("This job is Active and cannot be cancelled")//error message here
End If
End If
End If
Next

Catch ex As Exception
MyLog.WriteEvent("Unable to cancel pending jobs. " & ex.StackTrace & ex.Message)
Throw
Finally
If Not MyDR Is Nothing Then
MyDR.Close()
MyDR = Nothing
End If
If Not CancelQuery Is Nothing Then
CancelQuery.CloseConnection()
CancelQuery.Dispose()
CancelQuery = Nothing
End If
End Try
Response.Redirect("../PCMS_WebPages/ActualsCurJob.aspx")
End Sub


I'm getting the following error: "It is invalid to show a modal dialog or form when the application is not running in UserInteractive mode. Specify the service or DefaultDesktopOnly style to display a notification from a service application"

Needless to say that i'm working with a modaldialog box and when a user is cancelling a job that has a return value=1, i want the alert box to show that the job cannot be cancelled.

I'm also working with an ASP file where a table is dynamically created inside the modaldialog box:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="ActualsCurJob.aspx.vb" Inherits="PCMS.ActualsCurJob"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Current Job Status</title>
<base target="_self">
<%
' =================================================================================================
' Name: ActualsCurJob.aspx
'
' Description: This is an modal window that list the active and pending ADW Extract jobs
'
' Inputs: None
'
' Outputs: None
'
' Return Codes: None
'
' Objects Included: None
' =========================================================
=========================================================
%>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<LINK href="PCMS_Web.css" rel="stylesheet">
<script language="javascript">
function Disable()
{
//*********************************************************************************
//Description: Enable/Disable Cancel File button depending on whether a file is
// checked
//
//Input: None
//
//Output: None
//
//Return Code: None
//*********************************************************************************
var ID;
var frm = document.forms[1];
var xx = document.getElementById("tabJobList");
var icnt = xx.rows;
//var MyCookie = "";
//xxx=yyy;
for (var fileID=2; fileID <= icnt.length; fileID++)
{
ID = document.getElementById(fileID);
if(ID != null)
{
if(ID.checked)
{
document.forms("frmCurJobs").btnCancel.disabled = false;
break;
//MyCookie = MyCookie + ",";
}
else
{
document.forms("frmCurJobs").btnCancel.disabled = true;
}
}
}
}
function ShowMessage()
{
alert("This job is Active and cannot be cancelled");
}


</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="frmCurJobs" method="post" runat="server">
<TABLE height="100%" cellSpacing="0" cellPadding="0" width="100%" border="0">
<TR height="29">
<TD style="HEIGHT: 29px" width="11">&nbsp;</TD> <!--chasm between nav and tabs-->
<TD style="HEIGHT: 29px" vAlign="top" align="left">
<TABLE cellSpacing="0" cellPadding="0" border="0">
<TR>
<td><br>
&nbsp;<br>
&nbsp;</td>
</TR>
<TR>
<TD colSpan="5">
<% ' START OF HEADER %>
<TABLE cellSpacing="0" cellPadding="0" width="100%" bgColor="#330099" border="0">
<TR vAlign="bottom">
<% ' Start of the buttons %>
<TD vAlign="top" align="left">
<asp:button id="btnCancel" enabled="False" Text="Cancel Job" runat="server"></asp:button>
<asp:button id="btnRefresh" runat="server" Text="Refresh List"></asp:button>
</TD>
</TR>
</TABLE>
<% ' END OF HEADER %>
</TD>
</TR>
<TR>
<td></td>
<td colSpan="4">&nbsp;
</td>
</TR>
<tr>
<TD align="center" colSpan="5"><asp:panel id="pnlNoFiles" Runat="server" Visible="False"><STRONG><FONT color="red">No
Scheduled Jobs found.</FONT></STRONG>
</asp:panel><BR>
</TD>
</tr>
<TR>
<td></td>
<TD colSpan="4"><asp:panel id="pnlFileList" Runat="server" Visible="True">
<TABLE id="tabJobList" cellSpacing="0" cellPadding="0" width="100%" border="1" runat="server">
</TABLE>
</asp:panel></TD>
</TR>
<TR>
<td colSpan="5">
<hr SIZE="5">
</td>
</TR>
<tr>
<td></td>
<TD colSpan="4"><BR>
<!-- #INCLUDE file="../common/Footer.asp" --></TD>
</tr>
</TABLE>
</TD>
</TR>
</TABLE>
<INPUT id="PlnErrorMsg" type="hidden" name="PlnErrorMsg" runat="server"> <INPUT id="PlnErrorShow" type="hidden" name="PlnErrorShow" runat="server">
</form>
</body>
</HTML>

vinyl-junkie
07-19-2007, 11:56 PM
Here's how you should do your alert box:

MsgBox.show("This job is Active and cannot be cancelled")

debbie_lee104
07-20-2007, 12:49 AM
The MsgBox gets underlined with the following info:

Argument not specified for parameter 'prompt' of 'Public Function MsgBox(Prompt As Object, [Button As Microsoft.VisualBasic.MsgBoxStyle=MsgBoxStyle.Button1, [Title As Object=Nothing]) As Microsoft.VisualBasic.MsgBoxResult'

I then tried the following code but the MsgBox still get underlined:
MsgBox.show("This job is Active and cannot be cancelled", MsgBoxStyle.OKOnly)

vinyl-junkie
07-20-2007, 07:03 PM
Sorry about my faulty memory. I'm away from home at the moment and don't have access to my usual reference material. :o Here (http://msdn2.microsoft.com/en-us/library/139z2azd(VS.80).aspx) is an example of how those msgbox parameters should look.

miranda
07-20-2007, 07:41 PM
this MsgBox will appear on the server NOT the users machine (the client).

The use of a MsgBox in a client side script in an internet application is NOT recommended because it will only work in IE.

You will need to have your function call a client side action to alert the user.
the client side action is normally handled in Javascript for cross browser compatability.

debbie_lee104
07-23-2007, 04:37 PM
I've tried all things mentioned above and i still get the error message. I would like my message box to pop up from a modal dialog on a condition.
i.e.: If MyDR.RecordsAffected = 1 Then '1 indicates failure to cancel
MsgBox("This job is Active and cannot be cancelled")//error message here
End If
this doesn't work because the modal dialog is a child page (aspx.vb) which is above the parent page (aspx). I think the pop is looking for the parent page and can't find it. how can i make this work?

miranda
07-25-2007, 04:21 AM
I will repeat myself here. The MsgBox will NEVER appear on the client. It can't because this code is running on the server and NOT on the client. A msgbox does not work in ASP because it will always appear on the server that the web application is running on. Sorry, but the MsgBox function does NOT work in any version of ASP. Be it ASP classic or ASP.Net. If you don't believe me, Google it.

debbie_lee104
07-25-2007, 04:06 PM
I will repeat myself here. The MsgBox will NEVER appear on the client. It can't because this code is running on the server and NOT on the client. A msgbox does not work in ASP because it will always appear on the server that the web application is running on. Sorry, but the MsgBox function does NOT work in any version of ASP. Be it ASP classic or ASP.Net. If you don't believe me, Google it.

I do believe what you are saying but i can't get to make it work w/ a javascript code either. I have this function in my aspx file:
function ShowMessage()
{
alert("This job is Active and cannot be cancelled");
}

and then i can't seem to call it from the aspx.vb file. Do you have any examples to show?

i also tried the following and many others:
Dim message
message= This job is Active and cannot be cancelled

If MyDR.RecordsAffected = 1 Then '1 indicates failure to cancel
Response.Write("<script LANGUAGE=JavaScript>")
Response.Write("alert('" & message & "')")
Response.Write("</script>")

I understand what you are saying, but how do i do it? That is the question.

miranda
07-26-2007, 04:41 PM
Why not include this in your error handling? In this instance you can intentionally throw a user-defined exception and catch that exception in your catch block. The catch block can include the code to write the javascript code to display the alert.


'user-defined exception class
Public Class MyException Inherits System.Exception
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal Message As String)
MyBase.New(message)
End Sub
End Class




Try
'put your code in here
If MyDR.RecordsAffected = 1 Then '1 indicates failure to cancel
Throw New MyException("your exception here")
End If
Catch ex As MyException
Response.Write("<script LANGUAGE=JavaScript type=""text/javascript"">")
Response.Write("alert('" & ex.message & "')")
Response.Write("</script>")
Catch ex As Exception
'some other error occured
Finally

End Try